File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Garbage Collector magic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collector magic" Watch "Garbage Collector magic" New topic
Author

Garbage Collector magic

Burkhardt Hufnagel
Greenhorn

Joined: May 08, 2001
Posts: 14
As I understand it, only subclasses and classes in the same package can access a protected method.
If that's true then how does the Garbage Collector call an object's finalize() method when the signature is "protected void finalize() throws Throwable" ?
------------------
TANSTAAFL


TANSTAAFL
Laurent Leonard
Ranch Hand

Joined: May 15, 2001
Posts: 35
the call to System.gc()
is transfered to Runtime.getRuntime().gc();
Which is a native function implemented in the JVM.
In the JVM, you have a set of "garbage collectable" objects
which gc and then if they are not finilized already, a call is made to the finalization. Thus we are not more in Java itself but in a lower level.

------------------
Laurent Leonard
Laurent.Leonard@belgique.com


Laurent LEONARD
Burkhardt Hufnagel
Greenhorn

Joined: May 08, 2001
Posts: 14
Oh. Wait-a- minute... Your answer implies that native methods are not restricted by the access modifiers. That sounds like a Bad Thing.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The garbage collector is not a class. It does not have to live by the rules of classes, including access modifiers.
It is a part of the JVM itself. like the bootstrap classloader. It calls native code to do it's dirty work for it.
The System.gc() method that users can call is NOT actually the garbage collector. It is a method that notifies that gc that now would be a good time to run.
Native code can not get at classes without going through JNI which enforces access restrictions.
[This message has been edited by Cindy Glass (edited May 23, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
NIKHIL JAIN
Greenhorn

Joined: May 29, 2001
Posts: 6
first of all, it is important to clear that with a PROTECTED modifier, the SUB-classes of a different package can also access the method.
the garbage collector is a separate thread running at the backgroung. to be precise its a DAEMON thread. at what time it will garbage collect the waste objects, its unpredictable!!
Burkhardt Hufnagel
Greenhorn

Joined: May 08, 2001
Posts: 14
Thanks, Cindy. You've hit the nail on the head. I fell into the trap of thinking that since the GC is part of Java it must obey the same rules that our classes do.
------------------
TANSTAAFL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collector magic