51
\$\begingroup\$

Write a program which runs forever and allocates more and more memory on the heap the longer it runs, at least until you reach the limit of the operating system on the amount of memory that can be allocated.

Many kernels won't actually reserve memory you allocate until you use it for something, so if your program is in C or some other low level language you'll have to make sure you write something to each page. If you're using an interpreted language, you're probably not going to have to worry about this.

Shortest code wins.

\$\endgroup\$
8
  • 13
    \$\begingroup\$ Is a stack overflow a valid solution? Does the memory have to be leaked or just allocated? \$\endgroup\$
    – Wheat Wizard
    Dec 1, 2016 at 3:02
  • 1
    \$\begingroup\$ @WheatWizard The memory does not have to be leaked, but it has to be allocated faster than it is deallocated. \$\endgroup\$
    – tbodt
    Dec 1, 2016 at 17:16
  • 2
    \$\begingroup\$ The 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. \$\endgroup\$ Dec 2, 2016 at 1:05
  • 15
    \$\begingroup\$ Note to self: Save code before testing. Introducing mem-leaks might crash IDE... \$\endgroup\$
    – steenbergh
    Dec 2, 2016 at 9:03
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$ Dec 17, 2016 at 18:30

71 Answers 71

1 2
3
0
\$\begingroup\$

Java 104 bytes

import java.util.*;class A{public static void main(String[]a){List b=new ArrayList();for(;;)b.add(1);}}

This will allocate a List and let it grow constantly until it runs out of space.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It might be shorter to use the fully qualified name of the class instead of importing. (see codegolf.stackexchange.com/a/16100/10801 and all other answers at that page. useful tips en masse there) \$\endgroup\$
    – masterX244
    Dec 5, 2016 at 13:58
0
\$\begingroup\$

JavaScript - 14 18 bytes

while(x=[self.x]);

Based on the activity on @Lmis' answer, some fun with evaluating expressions in intermediate scopes. Didn't manage to avoid reference errors :'(!

\$\endgroup\$
0
\$\begingroup\$

Commodore 64

If ?STRING TOO LONG ERROR counts, then this will work on the Commodore 64 and VIC-20:

0 a$=a$+" ":goto

or if you need ?OUT OF MEMORY ERROR then:

0 dim a(255,255)

However, the last solution does not use a loop as the error is reported as soon as run is entered.

\$\endgroup\$
0
\$\begingroup\$

C#, 28 bytes

()=>{var _="a";for(;;)_+=_;}

What is there to say? It adds the contents of _ to _ forever. Throws OutOfMemoryException almost instantly.

\$\endgroup\$
0
\$\begingroup\$

c#, 55 bytes

class c{static void Main(){string c="";for(;;)c+='c';}}

Alternative 54 bytes

class c{static void Main(){string c="c";for(;;)c+=c;}}
\$\endgroup\$
0
\$\begingroup\$

Sinclair ZX80/ZX81 BASIC, ~14 bytes ~7 bytes (ZX81/ZX80 8K ROM), ~10 bytes ~5 bytes (ZX80 4K ROM)

 1 GOSUB 1

As there is never a return from the GOSUB (presented as GO SUB on the ZX80 with 4K ROM), the stack is filled which will eventually cause the interpreter to error. To clear the stack, simply enter NEW which should initiate a soft reset.

\$\endgroup\$
1
  • \$\begingroup\$ Actually one should be able to 1 GOSUB 1 - why didn't I think of that. \$\endgroup\$ Apr 5, 2017 at 18:42
0
\$\begingroup\$

VB.NET, 162 bytes

Module m
    Sub Main()
        Dim s = ""
        While (1)
            s = s + "1" : Threading.Thread.Sleep(2147483647)
        End While
    End Sub
End Module

It won't really run forever but close enough. It should take about 584942417 years to allocate 8gb.

\$\endgroup\$
0
\$\begingroup\$

Whitespace, 8 bytes


  

 	

Try it online!

Explanation

(s - space, t - tab, n - newline)

nssn ; Declares a label consisting of the empty sequence
nstn ; Call the procedure given by the empty label (declared above)

This is a slight abuse of the rules. The call procedure command jumps to the specified label and marks the current location for a later return. The mechanisms for doing this are implementation dependent but a basic implementation used by many interpreters is to push the current location to a call stack and pop values when a ret command is encountered. This will therefore end up filling up the call stack of the interpreter.


Whitespace, 12 bytes - Fills the stack


  
   

 


Try it online!

Explanation

nssn ; Declare label ''
sssn ; Push 0
nsnn ; Jump to label ''

Continuously pushes the value 0 to the stack, causing the stack to grow until the interpreter crashes.


Whitespace, 29 bytes - Fills the heap

   	

  
 
 	    
  
 		 
 


Try it online!

Explanation

ssstn ; push 1
nssn  ; label ''
sns   ; duplicate n
tsss  ; add (effectively multiplies by 2)
sns   ; duplicate n (to use as a heap address)
sns   ; duplicate n (to use as a value)
tts   ; store n at the address n
nsnn  ; jump ''

Fills heap addresses given by 2^n with the value of 2^n, starting at n=1. Consumes memory faster on interpreters that implement the heap as an array but will also work for interpreters that use a non-contiguous data structure backing the heap (such as a dictionary/map).

\$\endgroup\$
0
\$\begingroup\$

Implicit, 2 bytes

(a

Try it online! (Uses up all allowed TIO memory in about 1 second)

(   create jump point
 a  push 97
    implicit infinite loop
\$\endgroup\$
0
\$\begingroup\$

Flurry, 14 bytes

(<>{}{({})}){}

Adapted from this answer. One of the {} terms is replaced with {({})}, a function which pushes its argument to the stack. Thus the infinite loop grows the stack on every iteration.

\$\endgroup\$
0
\$\begingroup\$

Keg, 2 bytes

{a

Try it online!

Simply push infinite as

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.