I was trying this challenge, which requires you to write a program that continually consumes memory.
I figured this would be easy, so I wrote up:
(reduce conj [] (range))
Which basically adds numbers from an infinite range into a vector; theoretically forever.
The problem is, this jumps up to 3/4GB of memory, then just stops. The CPU is still running hard, but it refuses to grow any larger.
I decided to steal an idea from a Java answer and override finalize
to create more objects every time one is deallocated:
(defrecord A []
Object
(finalize [this] (mapv (fn [_] (A.)) (range 5)))) ; Create 5 more
(mapv (fn [_] (A.)) (range))
But this stops growing around the same point as well.
How is this not exploding? Especially since I'm using a strict map
, it should be retaining everything in memory shouldn't it? As far as it "knows", I'll want it to print the entire list at some point, so it needs to hold onto everything. Plus, if it was deallocating at some point, shouldn't the overridden finalize
method overcome this?
Can anyone explain what's going on here? I wrote this on my break, so I may be overlooking something, but I can't see what. It was tested in Intellij/Cursive's REPL.