locals clearing

১৪০টি ভিউ
প্রথম অপঠিত মেসেজটিতে চলে আসুন

Daniel Janus

পড়া হয়নি,
৩০ জানু, ২০১১, ৬:০৮:৫৬ AM৩০/১/১১
প্রাপক clo...@googlegroups.com
Hi,

I've recently heard about the locals clearing feature of Clojure 1.2 (from a recent post by Ken Wesson), and decided to test-drive it. Here is a contrived example:

(defn garbage []
  (make-array Byte/TYPE 10485760))

(defn -main [& args]
  (let [a (garbage)
        b (garbage)
        c (garbage)
        d (garbage)
        e (garbage)
        f (garbage)
        g (garbage)
        h (garbage)]
    (println "OK")))

Now, when I build this with Leiningen and try to run under -Xmx20M or so, it bombs out on me with an OOME. Changing the let to a bunch of nested lets doesn't help, nor does migrating to Clojure 1.3alpha4. 

Shouldn't the locals clearing feature detect that each of the a-h locals is no longer needed and clear them right after allocation? Is this a misconception on my part about how this works? Or is something weird going on here? 

Thanks,
-Daniel

Rich Hickey

পড়া হয়নি,
৩০ জানু, ২০১১, ৮:৪১:৪৪ AM৩০/১/১১
প্রাপক Clojure
Locals clearing happens at the point of last use. Since those locals
are never used, no clearing code is emitted. This java program fails
similarly:

public class Mem {
public static void main (String[] args) {
byte[] a = new byte[10485760];
byte[] b = new byte[10485760];
byte[] c = new byte[10485760];
byte[] d = new byte[10485760];
byte[] e = new byte[10485760];
byte[] f = new byte[10485760];
byte[] g = new byte[10485760];
byte[] h = new byte[10485760];

System.out.println ("Ok");
}
}

This works in Clojure due to locals clearing:

(defn garbage []
(make-array Byte/TYPE 10485760))

(defn -main [& args]
(let [a (garbage)
b (when a (garbage))
c (when b (garbage))
d (when c (garbage))
e (when d (garbage))
f (when e (garbage))
g (when f (garbage))
h (when g (garbage))]
(println "OK")))

Since unused locals serve no purpose, optimizing that case is not
going to become a priority any time soon. (Note: I still think the JVM
GC should be able to figure out that those locals are dead references.
I'm just not going to work around it in this case).

Rich

.Bill Smith

পড়া হয়নি,
৩০ জানু, ২০১১, ৮:৪৯:১২ AM৩০/১/১১
প্রাপক clo...@googlegroups.com
Am I correct in assuming that if those allocations had been smaller (i.e. if the JVM had not run out of memory), the GC would have eventually detected the dead references and freed them once the locals went out of scope?

Bill Smith

Michael Wood

পড়া হয়নি,
৩০ জানু, ২০১১, ১১:৩৩:১২ AM৩০/১/১১
প্রাপক clo...@googlegroups.com
Hi

On 30 January 2011 15:49, .Bill Smith <william...@gmail.com> wrote:
> Am I correct in assuming that if those allocations had been smaller (i.e. if
> the JVM had not run out of memory), the GC would have eventually detected
> the dead references and freed them once the locals went out of scope?

Yes, once they are out of scope they are eligible for garbage
collection. The "locals clearing" is just an optimisation to allow
the GC before they are out of scope, but after they are no longer
needed.

> On Sunday, January 30, 2011 7:41:44 AM UTC-6, Rich Hickey wrote:
>>
>> (Note: I still think the JVM
>> GC should be able to figure out that those locals are dead references.

--
Michael Wood <esio...@gmail.com>

সকলকে উত্তর দিন
লেখককে লিখে পাঠান
ফরওয়ার্ড করুন
0টি নতুন মেসেজ