{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/10240","labels_url":"https://api.github.com/repos/apache/spark/issues/10240/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/10240/comments","events_url":"https://api.github.com/repos/apache/spark/issues/10240/events","html_url":"https://github.com/apache/spark/pull/10240","id":121389490,"number":10240,"title":"[SPARK-12155] [SPARK-12253] Fix executor OOM in unified memory management","user":{"login":"andrewor14","id":2133137,"avatar_url":"https://avatars.githubusercontent.com/u/2133137?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewor14","html_url":"https://github.com/andrewor14","followers_url":"https://api.github.com/users/andrewor14/followers","following_url":"https://api.github.com/users/andrewor14/following{/other_user}","gists_url":"https://api.github.com/users/andrewor14/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewor14/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewor14/subscriptions","organizations_url":"https://api.github.com/users/andrewor14/orgs","repos_url":"https://api.github.com/users/andrewor14/repos","events_url":"https://api.github.com/users/andrewor14/events{/privacy}","received_events_url":"https://api.github.com/users/andrewor14/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-12-10T02:49:44Z","updated_at":"2015-12-10T17:52:19Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/10240","html_url":"https://github.com/apache/spark/pull/10240","diff_url":"https://github.com/apache/spark/pull/10240.diff","patch_url":"https://github.com/apache/spark/pull/10240.patch"},"body":"**Problem.** In unified memory management, acquiring execution memory may lead to eviction of storage memory. However, the space freed from evicting cached blocks is distributed among all active tasks. Thus, an incorrect upper bound on the execution memory per task can cause the acquisition to fail, leading to OOM's and premature spills.\r\n\r\n**Example.** Suppose total memory is 1000B, cached blocks occupy 900B, `spark.memory.storageFraction` is 0.4, and there are two active tasks. In this case, the cap on task execution memory is 100B / 2 = 50B. If task A tries to acquire 200B, it will evict 100B of storage but can only acquire 50B because of the incorrect cap. For another example, see this [regression test](https://github.com/andrewor14/spark/blob/fix-oom/core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala#L233) that I stole from @JoshRosen.\r\n\r\n**Solution.** Fix the cap on task execution memory. It should take into account the space that could have been freed by storage in addition to the current amount of memory available to execution. In the example above, the correct cap would have been 600B / 2 = 300B.\r\n\r\nThis patch also guards against the race condition (SPARK-12253):\r\n(1) Existing tasks collectively occupy all execution memory\r\n(2) New task comes in and blocks while existing tasks spill\r\n(3) After tasks finish spilling, another task jumps in and puts in a large block, stealing the freed memory\r\n(4) New task still cannot acquire memory and goes back to sleep"},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/163701557","html_url":"https://github.com/apache/spark/pull/10240#issuecomment-163701557","issue_url":"https://api.github.com/repos/apache/spark/issues/10240","id":163701557,"user":{"login":"andrewor14","id":2133137,"avatar_url":"https://avatars.githubusercontent.com/u/2133137?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewor14","html_url":"https://github.com/andrewor14","followers_url":"https://api.github.com/users/andrewor14/followers","following_url":"https://api.github.com/users/andrewor14/following{/other_user}","gists_url":"https://api.github.com/users/andrewor14/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewor14/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewor14/subscriptions","organizations_url":"https://api.github.com/users/andrewor14/orgs","repos_url":"https://api.github.com/users/andrewor14/repos","events_url":"https://api.github.com/users/andrewor14/events{/privacy}","received_events_url":"https://api.github.com/users/andrewor14/received_events","type":"User","site_admin":false},"created_at":"2015-12-10T17:52:19Z","updated_at":"2015-12-10T17:52:19Z","body":"Last commit actually passed tests last night."}}