{"action":"created","issue":{"url":"https://api.github.com/repos/sbt/sbt/issues/1223","repository_url":"https://api.github.com/repos/sbt/sbt","labels_url":"https://api.github.com/repos/sbt/sbt/issues/1223/labels{/name}","comments_url":"https://api.github.com/repos/sbt/sbt/issues/1223/comments","events_url":"https://api.github.com/repos/sbt/sbt/issues/1223/events","html_url":"https://github.com/sbt/sbt/issues/1223","id":30450082,"number":1223,"title":"Reducing PermGen usage in sbt","user":{"login":"cunei","id":686260,"avatar_url":"https://avatars.githubusercontent.com/u/686260?v=3","gravatar_id":"","url":"https://api.github.com/users/cunei","html_url":"https://github.com/cunei","followers_url":"https://api.github.com/users/cunei/followers","following_url":"https://api.github.com/users/cunei/following{/other_user}","gists_url":"https://api.github.com/users/cunei/gists{/gist_id}","starred_url":"https://api.github.com/users/cunei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cunei/subscriptions","organizations_url":"https://api.github.com/users/cunei/orgs","repos_url":"https://api.github.com/users/cunei/repos","events_url":"https://api.github.com/users/cunei/events{/privacy}","received_events_url":"https://api.github.com/users/cunei/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/sbt/sbt/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/sbt/sbt/labels/Community","name":"Community","color":"eb6420"}],"state":"closed","locked":false,"assignee":{"login":"eed3si9n","id":184683,"avatar_url":"https://avatars.githubusercontent.com/u/184683?v=3","gravatar_id":"","url":"https://api.github.com/users/eed3si9n","html_url":"https://github.com/eed3si9n","followers_url":"https://api.github.com/users/eed3si9n/followers","following_url":"https://api.github.com/users/eed3si9n/following{/other_user}","gists_url":"https://api.github.com/users/eed3si9n/gists{/gist_id}","starred_url":"https://api.github.com/users/eed3si9n/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eed3si9n/subscriptions","organizations_url":"https://api.github.com/users/eed3si9n/orgs","repos_url":"https://api.github.com/users/eed3si9n/repos","events_url":"https://api.github.com/users/eed3si9n/events{/privacy}","received_events_url":"https://api.github.com/users/eed3si9n/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/sbt/sbt/milestones/14","html_url":"https://github.com/sbt/sbt/milestones/0.13.9","labels_url":"https://api.github.com/repos/sbt/sbt/milestones/14/labels","id":1040287,"number":14,"title":"0.13.9","description":"","creator":{"login":"eed3si9n","id":184683,"avatar_url":"https://avatars.githubusercontent.com/u/184683?v=3","gravatar_id":"","url":"https://api.github.com/users/eed3si9n","html_url":"https://github.com/eed3si9n","followers_url":"https://api.github.com/users/eed3si9n/followers","following_url":"https://api.github.com/users/eed3si9n/following{/other_user}","gists_url":"https://api.github.com/users/eed3si9n/gists{/gist_id}","starred_url":"https://api.github.com/users/eed3si9n/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eed3si9n/subscriptions","organizations_url":"https://api.github.com/users/eed3si9n/orgs","repos_url":"https://api.github.com/users/eed3si9n/repos","events_url":"https://api.github.com/users/eed3si9n/events{/privacy}","received_events_url":"https://api.github.com/users/eed3si9n/received_events","type":"User","site_admin":false},"open_issues":0,"closed_issues":17,"state":"closed","created_at":"2015-03-26T16:08:44Z","updated_at":"2015-08-11T21:35:21Z","due_on":null,"closed_at":"2015-08-11T21:35:21Z"},"comments":14,"created_at":"2014-03-29T18:57:22Z","updated_at":"2016-03-01T14:36:43Z","closed_at":"2015-05-07T17:40:45Z","body":"I tracked down a PermGen leak in sbt, which can easily be worked around (although other PermGen leaks may exist).\r\n\r\nThe full story is below, but in short object finalizers are not called frequently enough by the VM (in jdk 6 and 7), causing classloaders to survive long past their time.\r\n\r\nThe solution is adding after the parts of runTask that create a new classloader, in order to launch Scala or other utilities, the sequence:\r\n\r\n```\r\n{ System.gc(); java.lang.System.runFinalization(); System.gc() }\r\n```\r\n\r\nThat must be done from an enclosing stack frame that no longer contains any reference, direct or indirect, to the generated classloader (note that escape analysis was not generally on by default in jdk 6).\r\n\r\nThe complete, albeit condensed explanation, is:\r\n\r\n* Each time sbt invokes the Scala compiler, by creating a new classloader, the classloader is not released.\r\n* the classloader is kept in the heap by means of a chain of links involving weak references. In particular, when the garbage collector runs and frees the referent from a weak reference, the referred instance may still being tracked if some finalization is still pending.\r\n* if finalization is not run promptly, the classloader cannot be freed, and its permgen cannot be released.\r\n* file descriptors, like FileInputStream or JarFile, are all finalizable, and sbt uses a lot of them.\r\n* the jdk 6 and 7 are quite lazy when running finalizers. If there is still heap space, they will not rush to complete finalization in order to free memory. The VMs will not notice that PermGen is slowly running out.\r\n* as a net result, I saw cases in which there are more than 47'000 file descriptors lying idle in the system, indirectly keeping classloaders from being reclaimed, and leading some code to crash despite being given 1GB of PermGen.\r\n* adding an explicit invocation to runFinalization(), following the exact sequence described above, will solve the problem. The first gc() causes the referent of the WeakReferences to be released, and tracks the newly freed finalizable objects. The runFinalization() call forces the jvm to deal with those instances immediately. Finally, the second gc() will deallocate them. Once the file descriptors are freed, the weak references are also freed, the classloader is freed, and therefore its PermGen space is in the end also freed.\r\n\r\nMore in general,  when using together weak references, multiple classloaders, and files (even if you close them), manual invocations to runFinalization() will be needed, or you will get leaks that may show up as \"Too many files\" or \"PermGen Error\". \r\n\r\nJava 8 replaces the PermGen with a \"Meta\" space, but it is still subject to leaks. However, it calls finalizers more promptly, which avoids this issue. Java 6 and Java 7 are both affected."},"comment":{"url":"https://api.github.com/repos/sbt/sbt/issues/comments/190744628","html_url":"https://github.com/sbt/sbt/issues/1223#issuecomment-190744628","issue_url":"https://api.github.com/repos/sbt/sbt/issues/1223","id":190744628,"user":{"login":"fommil","id":1914041,"avatar_url":"https://avatars.githubusercontent.com/u/1914041?v=3","gravatar_id":"","url":"https://api.github.com/users/fommil","html_url":"https://github.com/fommil","followers_url":"https://api.github.com/users/fommil/followers","following_url":"https://api.github.com/users/fommil/following{/other_user}","gists_url":"https://api.github.com/users/fommil/gists{/gist_id}","starred_url":"https://api.github.com/users/fommil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fommil/subscriptions","organizations_url":"https://api.github.com/users/fommil/orgs","repos_url":"https://api.github.com/users/fommil/repos","events_url":"https://api.github.com/users/fommil/events{/privacy}","received_events_url":"https://api.github.com/users/fommil/received_events","type":"User","site_admin":false},"created_at":"2016-03-01T14:36:43Z","updated_at":"2016-03-01T14:36:43Z","body":"could it be that the finalizers are in a different classpath? Just before we discard an old classpath, can we run its finalizers?"}}