aspose file tools*
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
Author

garbage collection

kumar abhay
Ranch Hand

Joined: Oct 02, 2001
Posts: 53
hi there,
i m having a problem regarding garbage collection,which is :
1.what is the condition which let go garbage collection ?
2.when our object is about to be garbage collected,that time finilize method calls.why?
3.access modifier with finilize method is
protected.why?
4.can we call oursalves garbage collection.if we can then how ?
i hope that u would help me out by clearing all of these concepts which r making confusions to me.
thanks in advance
kumar abhay
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
1.what is the condition which let go garbage collection ?
There is no guarantee that the GC will ever run during the lifetime of your application. The GC is implementation dependent. You cannot run it by yourself. You can invoke System.gc() but there is no guarantee that the GC will actually run. The API says that "Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse." The expend effort thing is very important. That means that when you invoke System.gc(), the JVM will actually try to run the GC but nothing for sure. For instance, if there are high-priority thread running, the GC will probably not run. It all boils down to how the thread scheduler is written (priority based, round-robin, time-sliced ???) and how the GC is implemented. But do not rely on the fact that the GC can be run whenever you want. That's not true. In fact, the API should more precise about that.
To answer briefly, there is no universal conditions that trigger the garbage collection.
2.when our object is about to be garbage collected,that time finilize method calls.why?
This is to allow your object to clean itself up. For instance, if it has a database connection still open, you may want to close it before the object is garbage collected. If you have handles to opened file, you may want to release them and close the streams to the file... The finalize method is just for cleaning purposes.
Object.finalize()
3.access modifier with finilize method is protected.why?
See 2.
4.can we call oursalves garbage collection.if we can then how ?
See 1.
Here are some resources you might find helpful:
SCJP forum: Good resources on Garbage Collection
[ March 30, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection