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.
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: -
public class Zombie {
-
private static Zombie zz= null;
-
public static void main(String[] args) {
-
Zombie z= new Zombie();
-
z= null; // object not reachable anymore
-
System.gc();
-
// assume that z's finalize method is called here
-
zz.areYouAlive();
-
}
-
protected void finalize() { Zombie.zz= this; }
-
public void areYouAlive() { System.out.println("boo!"); }
-
}
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
could u be more clearer????
thanks anyway.
kind regards.
dmjpro.
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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...
|
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: 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
|
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();
|
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: 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: 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: 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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |