How to increase the priority of FinalizerThread for collecting objects in the GC

I am tracking my Java application with a profiler to find out a memory leak. And I got a class that takes up almost 80% of the memory, which

java.lang.ref.Finalizer

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

Now can someone tell me how to increase the priority of FinalizerThread to assemble this object in the GC.

Another thing I ran into this problem on Linux with Linux kernel version 2.6.9-5.ELsmp (i386) and Linux 2.6.18-194.17.4.el5 (i386) , but it works fine (without OOM error ) on Linux 2.6.18-128.el5PAE (i386).

Is this a problem due to the Linux kernel? Is there any JVM variable to improve the priority of FinalizerThread?

Thanx in advance.

+5
source share
3 answers

To answer a question literally, you can do it. However, as shown below, this is likely to be pointless, esp, since Thread already has 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]

If you are not using or near 100% of all your cores, priority does not matter, because even the lowest priority will receive as many CPUs as it wants.

Note. On Linux, it will ignore elevated priorities if you are not root.

, . . , , . ( )

, , , , , finalize(), .

, , Linux. , , , . ( , , )

+2

. . , .

Java SE 7:

try (final Resource resource = acquire()) {
    use(resource);
}

Java SE 7:

final Resource resource = acquire();
try {
    use(resource);
} finally {
    resource.release();
}
+2

, FinalizerThread, finalize(), Thread.currentThread(), . , .

, . , . , , :

  • , ,
  • , finalizer.

(, , .)

But in any case, I think the best solution is to get rid of the methods finalize(). I bet they are doing something unnecessary ... or dodgy. In particular, using finalizers to restore resources from discarded objects is a poor way to solve this particular problem. (See Tom Hotin's answer.)

0
source

All Articles