-5

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?

Boann
  • 48,794
  • 16
  • 117
  • 146
sorour
  • 49
  • 6
  • 2
    How much heap space do you need? .Did you start the jvm with option -Xmx ? – stacker Jun 22 '19 at 19:30
  • I actually increased the xmx and xms values, However I am not pretty sure about the appropriate values for the memory of 128 GB . – sorour Jun 22 '19 at 19:44
  • Sprinkling your code with `System.gc()` won't fix memory leaks. And explicitly calling `System.gc()` is an anti-pattern: Java is - almost always - better than you at deciding when it should garbage collect. – Mark Rotteveel Jun 23 '19 at 07:38
  • Do you understand the differences between classes, methods, and objects? The sentence “*I have a large program consisting of a main class with thousands of iterations*” makes no sense at all. – Holger Jun 24 '19 at 11:16

2 Answers2

3

Java won't throw an OutOfMemoryError until after it's collected all possible garbage. So manually calling the garbage collector in this case is not going to help.

The main effect of sprinkling System.gc(); calls in every method is going to be to make everything very slow. A full garbage collection cycle is naturally a big operation, even if no new garbage is found.

Instead, increase the amount of memory you allocate to the JVM. And/or, redesign your program to hold less data in memory at a time.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • @sorour Generally we don't fix heap space error with GC full collect. Most of the time the GC is smart enough to free space when it is required. If you happen in such situation, solutions are somewhere else : Set the heap memory consequently or perform a memory analysis to understand the reason. Tools as JVisualVM may help for the latter, which could help you to redesign your application as suggested here. – davidxxx Jun 22 '19 at 19:48
  • Err. You know that calling system.gc doesn't force gc runs? That is nothing but a friendly request, and it is totally up to the gc implementation what to do about these requests... – GhostCat Jun 22 '19 at 19:58
1

You cannot really control Garbage collection, with "System.gc" you can mark the code for the collector to say "Hey, I got stuff that needs to be removed", but whether it runs GC or not is not in your hand. Also mind, that only Objects that have no references on it left are being removed permanently by GC.

Zesa Rex
  • 412
  • 1
  • 4
  • 16
  • well does making the reference variables equal to null free my memory? Actually I did that but I still faced with the same issue. How can I free my memory instead of calling garbage collector? – sorour Jun 22 '19 at 19:51
  • @sorour The GC will free objects which are not **reachable** from any running thread of your program. If you have objects which are still reachable, but which you do not need, setting their references to `null` allows the GC to free them. However, it is OK if old objects reference each other or themselves cyclically, and it is not necessary to set all those references to null; so long as they are not reachable by your main program, they will all be freed automatically. – Boann Jun 22 '19 at 20:01