473,324 Members | 2,581 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,324 software developers and data experts.

little bit of confusion about finalize method.

dmjpro
2,476 2GB
i know that the finalize method is called by JVM when an object is garbage collected.
right???
but the ultimate memory release done by native OS.

now my confusion is ... when it is called in reality?????

have a look at my code .....

import java.io.*;

class PariTest2
{
protected void finalize()
{
try{
FileWriter out = new FileWriter("d:/deb_web/lis/test_finalize.txt");
out.write("Yahoo ... Finalize method gets called");
out.close();
System.out.println("Yahoo ... Finalize method gets called");
}catch(Exception e){
System.out.println("DMJPRO: Error");
}
}
}

class FinalizeTest
{
public static void main(String a[])
{
System.out.println("Main Thread starts");
new PariTest2();
}
}
when i can see the effect of finalize method calling????
plz help.

kind regards.
dmjpro.
May 23 '07 #1
5 1669
JosAH
11,448 Expert 8TB
Finalize methods are a bit of a burden; the only thing that is guaranteed is that
they are invoked at most once and there's no guarantee when exactly they will
be invoked. But there's more misery going on; have a look at this:
Expand|Select|Wrap|Line Numbers
  1. public class Zombie {
  2.    private static Zombie zz= null;
  3.    public static void main(String[] args) {
  4.       Zombie z= new Zombie();
  5.       z= null; // object not reachable anymore
  6.       System.gc();
  7.       // assume that z's finalize method is called here
  8.       zz.areYouAlive();
  9.    }
  10.    protected void finalize() { Zombie.zz= this; }
  11.    public void areYouAlive() { System.out.println("boo!"); }
  12. }
This filthy trick is named 'resurrection': when the garbage collector calls the
finalize() method on an obect because it can't be reached anymore, the object
itself makes itself reachable again. Finalize methods are a mess.

kind regards,

Jos
May 23 '07 #2
dmjpro
2,476 2GB
could u be more clearer????

thanks anyway.

kind regards.
dmjpro.
May 23 '07 #3
JosAH
11,448 Expert 8TB
could u be more clearer????
What was not clear about my reply? I know the 'resurrection' problem is
complicated by itself, but it was clearly explained in my reply. Read it again.

kind regards,

Jos
May 23 '07 #4
dmjpro
2,476 2GB
i don't understand that ...... when the garbage collector calls the
finalize() method on an obect because it can't be reached anymore, the object
itself makes itself reachable again. Finalize methods are a mess.


kind regards.
dmjpro.
May 23 '07 #5
JosAH
11,448 Expert 8TB
i don't understand that ...... when the garbage collector calls the
finalize() method on an obect because it can't be reached anymore, the object
itself makes itself reachable again. Finalize methods are a mess.


kind regards.
dmjpro.
Just what it reads: when the garbage collector is invoked it attempts to find
objects that are not referenced anywhere anymore: they are garbage. The
garbage collector invoked the finalize() method on those objects, but in my
example the object to be garbage collected makes itself reachable again
(see the code example: the object sets the zz static variable to itself) so it
cannot be garbage collected anymore. This is somethings that feels like a
contradiction and that's why I mentioned this example; something to think
about. Finalizers are a mess.

kind regards,

Jos
May 23 '07 #6

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

Similar topics

8
by: celeong | last post by:
Hi, anybody can help me with this. I've created a singleton class, and now wants to add destructor to it. I know we can implement the IDisposable and also overrides the Finalize method (from...
2
by: Barry Anderberg | last post by:
I've been doing some reading about Finalize and garbage collection. I've learned that finalizing should be avoided because objects that have a finalize method require 2 (possibly more) itterations...
15
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
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...
7
by: SalamElias | last post by:
I started to use the Finalize sub in aspax page, I put a break point on one of the lines, hit run with debug, the pages dispalys without breaking on the line. 2nd question, do we need to use the...
4
by: BLUE | last post by:
I've read many articles including the one from Duff's blog but I've many doubts. public static myClass Instance { get { if (myClass.instance == null) myClass.instance = new myClass();
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...
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: 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)...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.