Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Increase FinalizerThread's priority to collect objects in GC

I have monitor my java application with profiler to know memory leak. And I got class which taking almost 80% of memory which is

java.lang.ref.Finalizer

Then I google it for above class and found great article http://www.fasterj.com/articles/finalizer1.shtml

Now can any one suggest me how do I increase FinalizerThread's priority to collect those object's in GC.

One more thing I am facing this issue on Linux with kernel version Linux 2.6.9-5.ELsmp (i386) and Linux 2.6.18-194.17.4.el5 (i386) but it's working fine (without OOM Error) on Linux 2.6.18-128.el5PAE (i386).

Is this issue because of Linux Kernels? Is there any JVM variable to improve FinalizerThread's priority?

Thanx in advance.

like image 797
user1041580 Avatar asked Nov 11 '11 11:11

user1041580


People also ask

How do I fix long garbage collection time?

One way is to increase the Java heap size. Look at the Garbage Collection subtab to estimate the heap size used by the application and change Xms and Xmx to a higher value. The bigger the Java heap, the longer time it is between GCs.

How does garbage collector know which objects to free?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

What triggers a full GC?

The reason that a Full GC occurs is because the application allocates too many objects that can't be reclaimed quickly enough. Often concurrent marking has not been able to complete in time to start a space-reclamation phase.

How do I lower my GC frequency?

An increase in the heap size will reduce GC frequency and increase GC duration, while a decrease in the heap size will increase GC frequency and decrease GC duration. To minimize both GC duration and frequency, it is recommended that you create short-lived objects as much as possible in your application.


1 Answers

To answer the question literally you can do this. However as outlined below, it is likely to be pointless, esp as the Thread already has a high priority.

for(Thread t: Thread.getAllStackTraces().keySet())
    if (t.getName().equals("Finalizer")) {
        System.out.println(t);
        t.setPriority(Thread.MAX_PRIORITY);
        System.out.println(t);
    }

prints

Thread[Finalizer,8,system]
Thread[Finalizer,10,system]

Unless you are using 100% of all your cores, or close to it, the priority doesn't matter because even the lowest priority will get as much CPU as it want.

Note: On Linux, it will ignore raised priorities unless you are root.

Instead you should reduce the work the finalizer is doing. Ideally it shouldn't have anything to do. One cause of high load in the finalizer is creating resources which should be closed but are being discarded. (leaving the finalizer to close the resource instead)

In short, you should try and determine what resources are being finalized and make sure they don't need to do anything when finalize() is called, ideally don't use this method at all.

It is possible that resources are taking slight longer to close on the older version of the Linux kernel. I would check that the hardware is identical as this could mean it takes longer to clean up resource. (But the real fix is to ensure it doesn't need to do this)

like image 133
Peter Lawrey Avatar answered Sep 23 '22 17:09

Peter Lawrey