File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
I have got a question regarding object finalization.

My Question is:
- When I set „tf = null“ why doesn’t it call the finalize method and print out
("Hey, I'm in TestFinalize finalize()")?
It seems it only calls the finalize() method when I set the reference to null
(tf = null) AND excplicitly suggest garbage collection with „System.gc()“.
WHY doesn’t it simply call finalize() method when i set the object reference
To null. Why do I have to call System.gc() additionally?

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
When an object is unreachable, it becomes *eligible* for gc'ing - that is, the next time the garbage collector runs, it *may* gc the object. The finalize method is called *immediately* before the object gets gc'ed and there is no guarantee to when or wether this will happen besides that it will happen before an OutOfMemoryError occurs. Even System.gc() does only *suggest* that the gc gets activated.
Did that help?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Garbage Collection
It's not a secret anymore!