Reputation: 90
I read about infinite loops and found out in some languages it will stop when stack overflows or reached the allotted maximum memory. Others will loop forever depending on the type of program and language. My question is, do java infinite loops stop? I'm just curious because java has a garbage collector, that reuse memory when there's memory leak in case of reaching allotted maximum memory and stack overflow.
Also,will this kind of infinite loop stop?
for( ; ; ){}
Upvotes: 3
Views: 2369
Reputation: 719199
My question is, do java infinite loops stop?
Some do, some don't. It depends on how the loop is implemented.
Basically the cases that you identified as stopping in other languages could also stop in Java ... depending on how you code them.
A stack overflow will occur with a loop that is implemented by infinite recursion. That is easier to "achieve" in Java than in some languages. (No tail call optimization.)
A heap overflow will occur if the loop repeatedly allocates objects that remain reachable ... so that the GC is unable to reclaim them.
(And there are various other ways that a seemingly infinite loop can terminate.)
I'm just curious because java has a garbage collector, that reuse memory when there's memory leak in case of reaching allotted maximum memory and stack overflow.
The garbage collector reclaims unreachable objects, but objects that are reachable (i.e. that that could be used by the program in the future) cannot be reclaimed.
The garbage collector does not deal with stack memory, so it is not relevant to stack overflow failures.
Also,will this kind of infinite loop stop?
No.
Strictly speaking, any loop is NOT infinite if it stops ... for any reason. That includes reasons such as stack overflow or memory exhaustion.
But even infinite loops are not truly infinite. Eventually the user is going to kill the looping program, the power will fail, the hardware will be scrapped ... or something like that.
Upvotes: 7
Reputation: 3391
An infinite loop can stop because of the memory if the memory grows in the loop. In this case, there is no memory grow.
But, if you create some new heavy objects in every iteration of the loop, then it will stop eventually due to a Memory Limit Exception.
Upvotes: 2
Reputation: 882028
Infinite loops do not stop, absent some other reason for the whole thing falling to pieces :-)
That's usually a shortage of resources, be it stack space in the case of infinite recursion, or running out of memory because you're allocating some (and not freeing it) in the loop.
Garbage collection only throws away garbage. Memory is not considered garbage if it can still be reached by your variables.
In other words, if your infinite loop adds an element to the end of a linked list each time through (and you don't lose access to the head), none of those nodes will be garbage-collected and you will eventually run out of memory.
That particular loop of yours:
for (;;) {}
won't actually stop since it's not using any resources. Of course, you can kill the process, or you may have a separate thread which uses resources. Both these (and other events) may result in the loop stopping but they're all outside the control of that loop.
Upvotes: 3