473,666 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Finalize objects in GC cycle

3 New Member
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" />
Aug 31 '07 #1
10 2407
JosAH
11,448 Recognized Expert MVP
Imagine the following artificial scenario:

Expand|Select|Wrap|Line Numbers
  1. public class A {
  2.    pivate B b;
  3.    public A(B b) { this.b= b; }
  4.    public void setB(B b) { this.b= b; }
  5.    public cycle(int i) { if (i > 0) b.cycle(--i); }
  6.    protected void finalize() { cycle(42); }
  7. }
  8. public class B {
  9.    pivate A a;
  10.    public B(A a) { this.a= a; }
  11.    public void setA(A a) { this.a= a; }
  12.    public cycle(int i) { if (i > 0) a.cycle(--i); }
  13.    protected void finalize() { cycle(42); }
  14. ...
  15. B b= new b(null);
  16. A a= new A(b);
  17. b.setA(a);
  18. a= null;
  19. b= null;
  20. // GC hit here ...
  21.  
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
Aug 31 '07 #2
ask4rishi
3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. public class A {
  2.    pivate B b;
  3.    public A(B b) { this.b= b; }
  4.    public void setB(B b) { this.b= b; }
  5.    public cycle(int i) { if (i > 0) b.cycle(--i); }
  6.    protected void finalize() { cycle(42); }
  7. }
  8. public class B {
  9.    pivate A a;
  10.    public B(A a) { this.a= a; }
  11.    public void setA(A a) { this.a= a; }
  12.    public cycle(int i) { if (i > 0) a.cycle(--i); }
  13.    protected void finalize() { cycle(42); }
  14. ...
  15. B b= new b(null);
  16. A a= new A(b);
  17. b.setA(a);
  18. a= null;
  19. b= null;
  20. // GC hit here ...
  21.  
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
Sep 3 '07 #3
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.
Sep 3 '07 #4
sumittyagi
202 Recognized Expert New Member
Imagine the following artificial scenario:

Expand|Select|Wrap|Line Numbers
  1. public class A {
  2.    pivate B b;
  3.    public A(B b) { this.b= b; }
  4.    public void setB(B b) { this.b= b; }
  5.    public cycle(int i) { if (i > 0) b.cycle(--i); }
  6.    protected void finalize() { cycle(42); }
  7. }
  8. public class B {
  9.    pivate A a;
  10.    public B(A a) { this.a= a; }
  11.    public void setA(A a) { this.a= a; }
  12.    public cycle(int i) { if (i > 0) a.cycle(--i); }
  13.    protected void finalize() { cycle(42); }
  14. ...
  15. B b= new b(null);
  16. A a= new A(b);
  17. b.setA(a);
  18. a= null;
  19. b= null;
  20. // GC hit here ...
  21.  
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???
Sep 3 '07 #5
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:

Expand|Select|Wrap|Line Numbers
  1. public class Zombie {
  2.    private static Zombie zombieHook;
  3.    public Zombie() { }
  4.    public void talk() { System.out.println("boo!"); }
  5.    protected void finalize() { zombieHook= this; }
  6.    public static void main(String[] args) {
  7.       new Zombie(); // don't refer to it
  8.       System.gc();
  9.       Thread.sleep(10000); // give GC a chance
  10.       Zombie.zombieHook.talk();
  11.    }
  12. }
  13.  
kind regards,

Jos
Sep 3 '07 #6
sumittyagi
202 Recognized Expert New Member
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:

Expand|Select|Wrap|Line Numbers
  1. public class Zombie {
  2.    private static Zombie zombieHook;
  3.    public Zombie() { }
  4.    public void talk() { System.out.println("boo!"); }
  5.    protected void finalize() { zombieHook= this; }
  6.    public static void main(String[] args) {
  7.       new Zombie(); // don't refer to it
  8.       System.gc();
  9.       Thread.sleep(10000); // give GC a chance
  10.       Zombie.zombieHook.talk();
  11.    }
  12. }
  13.  
kind regards,

Jos
Thanks Jos, I got the point.
Sep 3 '07 #7
sumittyagi
202 Recognized Expert New Member
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.
Sep 3 '07 #8
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
Sep 3 '07 #9
sumittyagi
202 Recognized Expert New Member
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!
Sep 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4138
by: MuZZy | last post by:
Hi, Consider i have a class: class CTest { public static int Counter = 0; public CTest() { <...> Counter ++;
0
1437
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...
4
5070
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
20
7499
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()
12
2110
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."
3
1946
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
4
1257
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...
8
1874
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
3
1761
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...
0
8443
marktang
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...
0
8356
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,...
0
8866
Oralloy
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...
0
8781
jinu1996
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...
1
8550
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,...
0
7385
agi2029
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...
0
5663
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();...
0
4198
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...
1
2769
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.