Forcing the Java virtual machine to run the garbage collector

I have a complex Java application running on a large dataset. The app is fast enough, but over time it seems to consume a lot of memory and slow down. Is there a way to start the JVM garbage collector without restarting the application?

+3


source to share


8 answers


No , you cannot collect trash. Even using

System.gc();  

      

You can just do a garbage collection request, but it is up to the JVM to do this or not.

Also the garbage collector is smart enough to collect unused memory when needed, so instead of forcing garbage collection, you should check if you are not handling objects incorrectly.
If you are handling objects incorrectly (for example, keeping a reference to unnecessary objects), there is hardly anything the JVM can do to free memory.

From Doc



The gc method invocation suggests that the Java Virtual Machine expends the effort of disposing of unused objects to make the memory they are currently available for rapid reuse. When control returns from a method call, the Java Virtual Machine has made every effort to free up space from any discarded objects.

Open the Bug regarding documentationSystem.gc()

The documentation for System.gc () is extremely misleading and does not refer to the recommended practice to never call System.gc ().

The choice of language leaves it unclear what the behavior is when System.gc () is called and what external factors will influence the behavior.

A few useful links to visit when you think you have to force the JVM to free memory 1. How garbage collection works
2. When System.gc () does nothing
3. Why is it bad practice to call System.gc ()?

Everyone says
1. You have no control over GC in Java, does System.gc()

n't even guarantee it.
2. In addition, its poor practice as coercion can negatively impact productivity.
3. Revise your design and let the JVM do its job :)

+14


source


you shouldn't be relaying to System.gc()

- if you feel like you need to get the GC to work, that usually means there is something wrong with your code / design. The GC will start and clean up unused objects if they are ready to be instantiated - please check your design and think more about memory management, also look at loops in object references.



+1


source


System.gc() 

      

call in java, prompt vm to start garbage collection. Although this does not guarantee that he will actually do it. However, you have the best solution. As mentioned in other answers, jvisualvm utility (present in JDK since JDK 6 7 update) also provides garbage functionality.

EDIT:

your question will open my appetite for the topic and I came across this resource:

oracle gc resource

+1


source


The app is fast enough, but over time it seems to consume a lot of memory and slow down.

These are classic features of Java memory. It is likely that there is a data structure somewhere in your application that just keeps growing. As the heap gets close to full, the JVM spends more and more of its time GC (useless) trying to rip out some space.

Forcing the GC will not fix it because the GC is unable to collect the data structure. In fact, forcing GC execution just slows down the application.

The fix for the problem is to find what is causing the memory leak and fix it.

+1


source


Performance / performance degradation depends on how often you need garbage collection and how much memory your jvm has and how much your program needs.

When System.gc () is called, there is no definite certainty (its just a hint of a translator) of garbage collection, but at least it does. With enough calls, you can achieve some statistically significant performance multiplier just for your system.

The graph below shows the approximate consumption of programs, and jvm was given only 1GB (no gc), 1GB (gc), 3GB (gc), 3GB (no gc) respectively for each test.

In the beginning, when the jvm was only given 1GB of memory, whereas the program needed 3.75GB, it took more than 50 seconds for the producer thread pool to complete its work because less garbage management leads to a slower object creation rate.

The second example is about% 40 faster because System.gc () is called between each production of 150MB object data.

In the third example, jvm gets 3GB of memory when System.gc () is saved. More memory gave more performance as expected.

But when I disabled System.gc () in the same 3GB environment, it was faster!

Even if we can't force it, we might end up with some percentage gain or loss in performance trying System.g () if we try long enough. At least on my Windows 7 64 bit operating system with the latest jvm.

enter image description here

+1


source


The garbage collector starts automatically. You cannot force the garbage collector.

0


source


I am not suggesting that you do this, but to get the garbage collector to work from your java code you can just use all the available memory, it works because the garbage collector will run before the JVM throws an OutOfMemoryError ...

  try {
        List<Object> tempList = new ArrayList<Object>();
        while (true) {
            tempList.add(new byte[Integer.MAX_VALUE]);
        }
    } catch (OutOfMemoryError OME) {
       // OK, Garbage Collector will have run now...
    }

      

0


source


My answer will be different from others, but it will lead to the same question. Explain: YES, you can force the garbage collector to use two methods at the same time and in the same order:

System.gc ();

System.runFinalization ();

calling these two methods will cause the garbage collector to execute the finalize () method of any unreachable object and free the memory. however, software performance will be significantly degraded because garbage runs on its own thread and is not monitored and, depending on the algorithm used by the garbage collector, may result in unnecessary processing. Better if you check your code because it must be broken, you need to use a garbage collector to work efficiently.

NOTE. Just remember that this will only work if the finalize method is not a reassignment of the object, if it does, the object will continue to live and it will technically have a resurrection.

0


source







All Articles