Search code examples
javamemoryheap-memoryinfinite-loop

Not sure how and where I am stuck in this infinite loop


I am getting a java.lang.OutOfMemoryError: Java heap space exception in my code. I tried putting in print statements to see if I am stuck in an infinite loop. I know that my last loop is in the infinite loop as when i set the variable "i" to count how many iterations I had to stop my program when it reached 250000. Why am i stuck in this infinite loop and how would I fix it?


Solution

  • I think you are actually getting into an infinite loop because of the line

    temporaryOrderSampleSet.add(orderSample);
    

    You keep adding orderSample objects to this set and then immediately check all the members of that same set. So you start with 1 and check that 1, then the second is added and you check 1 and 2, then the 3 is added and then you check 1, 2 and 3, etc...