Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

This question already has an answer here:

Your mission (if you choose to accept it) is to create a memory leak. In a vein similar to this question, you must write a program that will continuously allocate/consume memory until the system runs out and begins swapping. However, in this contest, the objective is not to create a persistent memory leak, but to even create one in the first place!

Rules:

  1. You may use no fork bombs of any kind; e.g. no :(){ :|:&};:.
  2. The program must not be multi-threaded.
  3. The program must not call external programs.
  4. The program must not use any kind of looping. This not only includes for and while statements, but also includes goto statements, iterators, and recursion.
  5. The program must continually allocate memory till the end of time - Allocating a single large amount doesn't count.
  6. Standard loopholes apply.

This is an underhanded contest, so feel free to try to loophole your way around my rules.

share|improve this question

marked as duplicate by Kevin L, Martin Büttner, Dennis, Ourous, overactor Aug 26 at 22:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@KevinL Not so sure that's a duplicate; this one disallows loops. They're pretty close though. –  Doorknob 冰 Aug 26 at 21:04
1  
All of those questions use some type of looping. With looping disallowed, the problem is much harder. –  dr_andonuts Aug 26 at 21:12
    
Most of looping is just a short hand way of writing the same code over and over again. Couldn't i just write if(i == 0); i++; if(i == 0); i++; if(i == 0); i++; ad nauseam? –  Moop Aug 26 at 21:23
10  
Maybe I'm shortsighted, but I can't see how a program can run until "the end of time" without looping. It either loops back or it will halt "eventually". –  Geobits Aug 26 at 21:27
1  
Wouldn't a single self-recursing function within a program trivially satisfy the spec? It would consume infinite stack space. Or is this considered looping, or a fork bomb? Agreed with @Geobits that depending on how broad you make the definition of "loop", the very existence of a non-terminating program implies the existence of a loop. –  COTO Aug 27 at 5:23

2 Answers 2

Python, Linux

Take an infinite "file" and save it into memory

with open("/dev/random") as f:
    data = f.readLines()
share|improve this answer
2  
In python, without using any external resources, you can simply leak memory (infinitely) using itertools.count i.e. list(__import__("itertools").count()). You can extend it to other itertools functions like repeat i.e. list(__import__("itertools").repeat([0])) and cycle i.e. list(__import__("itertools").cycle([0])) –  Abhijit Aug 27 at 17:28
1  
I think /dev/random would count as an external resource and using itertools would count as a loop. –  britishtea Sep 14 at 0:40

Haskell

a simple program to calculates the average of a given list.

average :: [Integer] -> Float
average l = fromIntegral (sum l) / fromIntegral (length l) --fromIntegral is a casting function. ignore it.
main = print $ average $ repeat 5 -- the avrage of only 5's should be 5.

thare are two different interacting problems here. can you spot them?

this should be obvious to anyone who learned a bit haskell. the repeat function makes an infinite list. the computer can't compute neither the length nor the sum of an infinite list, so this will obviously not terminate. but what causes the space leak?

the second spoiler:

even if non infinite input was given, there would be a space leak. why? due to Haskell's lazy nature, lists are linked lists, which allows a function which iterates through a list to use constant space, as the start, the evaluated-but-already-used part is eaten by the GC and the tail is still unevaluated. but in the average function, while the computer computes sum l a reference to l is kept so that length l could be calculated; this makes the GC unable to collect the part already used by sum (which in this example just grows infinitely), and therefor a space leak.

share|improve this answer
    
Nice! I noticed the repeat, but I never knew that haskell lists had that behavior. –  dr_andonuts Aug 26 at 21:47
    
isn't repeat a loop? –  Moop Aug 26 at 21:55
    
no, i don't regard it as one. imagine a stream object in javascript which looks like {nextVal : function(){return 5} }. is this looping? –  proud haskeller Aug 26 at 21:59
    
@Moop and also, i really don't think there is a way to do this without looping. how can a program run infinitely without looping? the best that can be done is to include looping implicitly by using other functions. –  proud haskeller Aug 26 at 22:01
    
@proudhaskeller An infinite amount of statements will run for an infinite amount of time –  Moop Aug 26 at 22:40

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