It is my understanding that the default ClassLoader used in Java (and thus, Clojure) holds on to pointers to any anonymous classes created, and thus, onto lambdas and closures. These are never garbage collected, and so represent a "memory leak". There is some investigation going on for Java 7 or 8 (https://blogs.oracle.com/jrose/entry/anonymous_classes_in_the_vm) to adding an anonymous ClassLoader that will not retain references to these functions. In the mean time how are people dealing with writin开发者_运维问答g long-running applications in languages like Clojure and Scala, that encourage the use of these constructs?
Is there any possibility that Clojure could provide its own anonymous ClassLoader, extending the system one, but not holding onto created classes?
From bendin's comment above, and information from The Joy of Clojure, by Michael Fogus and Chris Houser, in the section "Compile-time vs. Run-time" (Chapter 7, Section 7.2), Fogus and Houser explain that closures and anonymous functions are compiled to byte-code at compile time and each call to the higher-order function that returns the closure, simply returns a new instance of the closure class, and not a new class. These instances will, of course, be garbage collected. Since there is an obvious, compile-time, upper bound on the number of anonymous functions and closures, memory will infrequently, if ever, be an issue.
My worries were unfounded.
精彩评论