473,609 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

little bit of confusion about finalize method.

dmjpro
2,476 Top Contributor
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.t xt");
out.write("Yaho o ... Finalize method gets called");
out.close();
System.out.prin tln("Yahoo ... Finalize method gets called");
}catch(Exceptio n e){
System.out.prin tln("DMJPRO: Error");
}
}
}

class FinalizeTest
{
public static void main(String a[])
{
System.out.prin tln("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 1684
JosAH
11,448 Recognized Expert MVP
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 Top Contributor
could u be more clearer????

thanks anyway.

kind regards.
dmjpro.
May 23 '07 #3
JosAH
11,448 Recognized Expert MVP
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 Top Contributor
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 Recognized Expert MVP
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
21083
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 the MSDN example). But is it possible that we just implement the Dispose method, which we will call at the end of the application? Will there be any memory leak or side effects?
2
2077
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 of the garbage collector to run before the memory is returned to the heap. The first time the GC runs the Finalize method is called, then the second time the memory is actually freed. The problem is that most of the major classes in the .NET...
15
2085
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 reference to some of the ClassA objects created in ClassB. In ClassC I hook into the ClassA events with a foreach loop so that I hook each object. The code is something like this: class ClassC { void SomeMethod() { foreach (ClassA item in...
4
5067
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
7496
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
2107
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."
7
1712
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 keyword "set" as normal vb to set an object to nothing or it is enough todo the following "myobject = nothing" instead of "set myobject = nothing " Thanks
4
2023
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
1871
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
0
8139
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
8091
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
8579
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...
1
8232
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
8408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7024
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...
1
6064
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.