Hi ,
We are monitoring the applications using the verboseGC. And in our application we have large number of finalize method. We have observed through VerboaseGC logs that lots of Finalize objects has queued up on each GC cycle.
And We know that the garbage collector needs a minimum of two cycles (maybe more GC cycles) to reclaim Finalize objects and needs to retain all other objects reachable from them during this process. But is there any other way to tune this problem irrespacting of rewrite the application code again.
Please help me on this.
VerboaseGC logs:
Few sample of finalization objects queued parameter in GC logs:
<finalization objectsqueued=" 198" />
<finalization objectsqueued=" 3960" />
<finalization objectsqueued=" 1102" />
<finalization objectsqueued=" 690" />
<finalization objectsqueued=" 63" />
<finalization objectsqueued=" 206" />
<finalization objectsqueued=" 75" />
<finalization objectsqueued=" 1034" />
<finalization objectsqueued=" 740" />
<finalization objectsqueued=" 684" />
<finalization objectsqueued=" 1110" />
<finalization objectsqueued=" 762" />
<finalization objectsqueued=" 1142" />
10 2407 JosAH 11,448
Recognized Expert MVP
Imagine the following artificial scenario: -
public class A {
-
pivate B b;
-
public A(B b) { this.b= b; }
-
public void setB(B b) { this.b= b; }
-
public cycle(int i) { if (i > 0) b.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
public class B {
-
pivate A a;
-
public B(A a) { this.a= a; }
-
public void setA(A a) { this.a= a; }
-
public cycle(int i) { if (i > 0) a.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
...
-
B b= new b(null);
-
A a= new A(b);
-
b.setA(a);
-
a= null;
-
b= null;
-
// GC hit here ...
-
At the end of that piece of code both a and b are eligable for garbage collection.
For one of them the finalize() method is invoked; the other one can't be collected
yet. Depending on which finalize() method was invoked first none of the other
objects can be collected. Even after the first finalizer is ready, the object can't
be collected because the *other* finalizer still needs it. Finalizers are a mess ...
kind regards,
Jos
Hi Jos,
Thank you very much for your kind suggestion. This means there is no other alternative except change the application. What do you suggest? Is there any efficient way to change the finalize method?
Thanks and regards,
Rishi.
Imagine the following artificial scenario: -
public class A {
-
pivate B b;
-
public A(B b) { this.b= b; }
-
public void setB(B b) { this.b= b; }
-
public cycle(int i) { if (i > 0) b.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
public class B {
-
pivate A a;
-
public B(A a) { this.a= a; }
-
public void setA(A a) { this.a= a; }
-
public cycle(int i) { if (i > 0) a.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
...
-
B b= new b(null);
-
A a= new A(b);
-
b.setA(a);
-
a= null;
-
b= null;
-
// GC hit here ...
-
At the end of that piece of code both a and b are eligable for garbage collection.
For one of them the finalize() method is invoked; the other one can't be collected
yet. Depending on which finalize() method was invoked first none of the other
objects can be collected. Even after the first finalizer is ready, the object can't
be collected because the *other* finalizer still needs it. Finalizers are a mess ...
kind regards,
Jos
JosAH 11,448
Recognized Expert MVP
Hi Jos,
Thank you very much for your kind suggestion. This means there is no other alternative except change the application. What do you suggest? Is there any efficient way to change the finalize method?
Thanks and regards,
Rishi.
I'd say don't implement finalizers; for real resources (open files, sockets etc.)
close them as soon as you're done with them; don't delay that to the finalization
stage. If you're done with a POJO (Plain Old Java Object) you could set its
reference to null a.s.a.p. but don't use finalizers; as I wrote: they're a mess.
Imagine the following artificial scenario: -
public class A {
-
pivate B b;
-
public A(B b) { this.b= b; }
-
public void setB(B b) { this.b= b; }
-
public cycle(int i) { if (i > 0) b.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
public class B {
-
pivate A a;
-
public B(A a) { this.a= a; }
-
public void setA(A a) { this.a= a; }
-
public cycle(int i) { if (i > 0) a.cycle(--i); }
-
protected void finalize() { cycle(42); }
-
}
-
...
-
B b= new b(null);
-
A a= new A(b);
-
b.setA(a);
-
a= null;
-
b= null;
-
// GC hit here ...
-
At the end of that piece of code both a and b are eligable for garbage collection.
For one of them the finalize() method is invoked; the other one can't be collected
yet. Depending on which finalize() method was invoked first none of the other
objects can be collected. Even after the first finalizer is ready, the object can't
be collected because the *other* finalizer still needs it. Finalizers are a mess ...
kind regards,
Jos
That means if there is an unreferenced object graph with 10 objects, then that graph will be collected in 10 gc cycles???
JosAH 11,448
Recognized Expert MVP
That means if there is an unreferenced object graph with 10 objects, then that graph will be collected in 10 gc cycles???
Nope, all those objects can be reaped in one sweep; unless there are finalizer
methods; all objects have to be kept intact (the finalizer might reference them)
until all finalizers are done.
There's even a nastier situation, called "resurrecti on"; here's an example: -
public class Zombie {
-
private static Zombie zombieHook;
-
public Zombie() { }
-
public void talk() { System.out.println("boo!"); }
-
protected void finalize() { zombieHook= this; }
-
public static void main(String[] args) {
-
new Zombie(); // don't refer to it
-
System.gc();
-
Thread.sleep(10000); // give GC a chance
-
Zombie.zombieHook.talk();
-
}
-
}
-
kind regards,
Jos
Nope, all those objects can be reaped in one sweep; unless there are finalizer
methods; all objects have to be kept intact (the finalizer might reference them)
until all finalizers are done.
There's even a nastier situation, called "resurrecti on"; here's an example: -
public class Zombie {
-
private static Zombie zombieHook;
-
public Zombie() { }
-
public void talk() { System.out.println("boo!"); }
-
protected void finalize() { zombieHook= this; }
-
public static void main(String[] args) {
-
new Zombie(); // don't refer to it
-
System.gc();
-
Thread.sleep(10000); // give GC a chance
-
Zombie.zombieHook.talk();
-
}
-
}
-
kind regards,
Jos
Thanks Jos, I got the point.
Thanks Jos, I got the point.
But one silly question was striking my mind. is't it the case all the finalize methods get called in one gc cycle of that graph??? for eg. the A and B class you gave in you example, can't gc run both the methods in one cycle. I think there won't be any inconsistency due to that. I know this question is sounding silly, but please guide me in this context.
JosAH 11,448
Recognized Expert MVP
But one silly question was striking my mind. is't it the case all the finalize methods get called in one gc cycle of that graph??? for eg. the A and B class you gave in you example, can't gc run both the methods in one cycle. I think there won't be any inconsistency due to that. I know this question is sounding silly, but please guide me in this context.
Sure, all finalizers can be run in separate threads if the GC decides to do so;
but imagine if all finalizers synchronize on one object; that little nasty scenario
I sketched above forces the GC to postpone the actual reaping until all finalizers
are finished (sequentially). Finalizers can slow down the GC considerably and
therefore slow down the user threads as well. As I wrote: it's better *not* to
implement finalizers.
kind regard,
Jos
Sure, all finalizers can be run in separate threads if the GC decides to do so;
but imagine if all finalizers synchronize on one object; that little nasty scenario
I sketched above forces the GC to postpone the actual reaping until all finalizers
are finished (sequentially). Finalizers can slow down the GC considerably and
therefore slow down the user threads as well. As I wrote: it's better *not* to
implement finalizers.
kind regard,
Jos
Great!! got the point!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: MuZZy |
last post by:
Hi,
Consider i have a class:
class CTest
{
public static int Counter = 0;
public CTest()
{
<...>
Counter ++;
|
by: ScriptNFool |
last post by:
Ok... I've mooched off this group for a while now. Time to give some
code back.
Here's a class I wrote to solve the problem of handling query strings.
I use it for an application where the page needs to preserve the
existing QueryString, but add or remove a couple items and then
redirect it back to the same page or other locations.
....A good example of how polymorphism can be useful for us VB'ers who
are less familiar with the...
|
by: Joe Abou Jaoude |
last post by:
I m preparing to pass the 70-306 exam, so i downloaded Q & A
from multiple sites.
There's this question that really confuses me, coz i see that both
answers A and C are both correct.
Can anyone help me to pick the right answer and explain me why ?
thx.
Your development team creates an order entry application by using
Visual Studio .NET. The application stores and retrieves data in a
|
by: Charles Law |
last post by:
I have an application that creates a class. The class has unmanaged
resources, so must end gracefully.
How can I guarantee that the unmanaged resources are freed?
I have looked at IDisposable, but this seems to rely on a call from the
application, e.g.
MyClass.Dispose()
|
by: Joe Abou Jaoude |
last post by:
hi,
I have a component that uses a database connection.
In the finalizer I dispose the connection because I read in msdn the
following:
"A type must implement Finalize when it uses unmanaged resources such as
file handles or database connections that must be released when the
managed object that uses them is reclaimed."
| |
by: Boni |
last post by:
Dear all,
can somebody explain difference between Dispose and Finalize and when each
of them should be used?
Thank you very much,
Boni
|
by: Larry Lard |
last post by:
I still don't really 'get' this. Why can't an object just release its
resources in its Finalize method? Why when I create (eg) a Graphics
object does it become *my* responsibility to say when I've finished
with it, and explcitly say so? I thought one of the points of managed
memory / garbage collection was that users of objects didn't have to
worry about tracking object lifetime?
What's the point of me saying g.Dispose at the end of my...
|
by: Rob |
last post by:
This is a weird one... I've got a class called PageInfo that has the
following finalize code:
Protected Overrides Sub Finalize()
MyBase.Finalize()
Do While m_TempFolders.Count
Dim TempPath As String = m_TempFolders.Dequeue
Granite.DeleteFolder(TempPath)
Loop
End Sub
|
by: Victory |
last post by:
Hi,
I just used VS2008 and migrated a project from 2005 to
2008. The reason is that MSDN docs is saying the Image
object in .NET Framework 3.5 has a Finalize method, which i
want to try out since my app is processing a few image
files and then saying it is running out of memory. I am
using the Dispose method of this object to get rid of it
and then setting it to Nothing. in any case, when i used VS
2008, intellisense still does not show...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |