I have a large program consisting of a main class with thousands of iterations. The code calls four methods through each iteration and saves the results of each method in ArrayLists and finally reports the best solution among the results.
I wanted to put my main class in a for loop and run my code for 20 times. First I faced a heap space error. I used the garbage collector to solve the error like this: At the end of the main class, (after making the unnecessary ArrayLists=null) I put "System.gc".
After 5 runs again I faced the heap space error but this time to solve the problem, I put "System.gc" inside each method but my code ran for hours without terminating when it should be finished within 5 or 6 minutes! How can I fix the problem?