473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with Finalize objects in GC cycle

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 2391
JosAH
11,448 Expert 8TB
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
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 Expert 8TB
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 Expert 100+
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 Expert 8TB
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 "resurrection"; 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 Expert 100+
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 "resurrection"; 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 Expert 100+
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 Expert 8TB
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 Expert 100+
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
Thank you very much for ur response.
With regards,
Rishi.
Sep 7 '07 #11

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

Similar topics

3
by: MuZZy | last post by:
Hi, Consider i have a class: class CTest { public static int Counter = 0; public CTest() { <...> Counter ++;
0
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...
4
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...
20
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,...
12
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...
3
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
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...
8
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...
3
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.