Ceylon, 55 54 35 bytes
shared void r(){variable Object x=0;while(0<1){x=[x];}}
This is a simple while loop, which creates a one-element tuple containing the previous value, i.e. an ever-growing linked list.
I get an OutOfMemoryError (with the default JVM settings on my computer) after:
real 5m42.727s
user 21m31.200s
sys 0m3.472s
5m42s (using 21 minutes of processor time, says time
).
Even shorter is the "functional" approach (which in effect does exactly the same):
shared void r(){loop<Object>(0)((x)=>[x]).each(noop);}
Unfortunately I need the type parameter here, otherwise Ceylon assumes Integer
(which is the type of 0
) and complains that [0]
is not an Integer.
The second version is much slower (I've let it run overnight):
real 197m26.677s
user 770m9.300s
sys 1m23.744s
A different approach is just filling a really big sequence of integers:
shared void r(){max((1:9^9)*.not);}
1:9^9
is a Measure<Integer>
(an Iterable containing 9^9 elements, starting with 1), by itself not having lots of memory (just the two numbers). But the *.not
attribute spread creates a big ArraySequence with the value of i.not
for each element of the Iterable. We need to do something with the result (otherwise the compiler complains that this is not a statement), therefore the max(...)
. The max
function is never called here, because this gives an OutOfMemoryError (with "GC overhead limit exceeded") after 6 minutes while trying to build and fill the array:
real 6m23.948s
user 23m46.956s
sys 0m5.200s
13Is a stack overflow a valid solution? Does the memory have to be leaked or just allocated? – Post Rock Garf Hunter – 2016-12-01T03:02:39.910
1@WheatWizard The memory does not have to be leaked, but it has to be allocated faster than it is deallocated. – tbodt – 2016-12-01T17:16:53.130
2The one time I want my program to consume infinite memory, I can't get it to.
(reduce conj [] (range))
(Clojure) gets up to 737mb, then just stops growing. Idk how it's not continually going up. It "thinks" I want to print the entire list at the end, so it shouldn't be throwing anything away. Very frustrating. – Carcigenicate – 2016-12-02T01:05:39.41014Note to self: Save code before testing. Introducing mem-leaks might crash IDE... – steenbergh – 2016-12-02T09:03:00.553
@steenbergh Ya, happened to me too. At least a 10 bytes solution is easy to rewrite :D – Carcigenicate – 2016-12-02T15:02:54.430
Would a program that computes a googolplex count? It wouldn't run "forever" in the technical sense, but it would use up all the resources of any computer. – asmeurer – 2016-12-03T18:09:27.590
@asmeurer I guess so... – tbodt – 2016-12-03T18:13:26.090
1I think you should add another golf challenge, similar but separate to this, requiring the program consume memory faster than a linear function of time. For the current challenge, looping forever and allocating a single byte should be fine. For your new challenge, that would be insufficient, but looping forever and doubling the amount of memory used each time would be ok. – BenGoldberg – 2016-12-17T18:30:47.523