aspose file tools*
The moose likes Beginning Java and the fly likes need clarification on 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 "need clarification on garbage collection " Watch "need clarification on garbage collection " New topic
Author

need clarification on garbage collection

dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
1)How can you force garbage collection?
[a] Garbage collection cannot be forced
[b] Call System.gc().
[c] Call Runtime.gc().
[d] Set all references to null.

The answe i feel is (b) call System.gc()
But the answer shown in someother site is (a) garbage collection cannot be forced

2). From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
[a] 1
[b] 2
[c] 3
[d] 0
plz let me know the answer for this...
help is appreciated
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,
Welcome to JavaRanch.
The answer to the first question is indeed, a. System.gc() is just a suggestion to the Java Virtual Machine, which can be ignored.
The answer to the second question is 0. The only string objects here are String literals, and they are stored in an internal table, and are not GC'd.


[Jess in Action][AskingGoodQuestions]
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
If I could try to clarify to make sure I understand... when an object has been released ( either set to null or GC suggested, whatever ), Java will/can clean this stuff up on it's next pass. All you can do is mark stuff for collection - right? There is no way to force it whatsoever?


David Crossett
-nothing important to say, but learnin' plenty-
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Sorry David buts that's what the JVM spec says. You can set a reference to null, and the object is eligible for garbage collection, but you cannot force it. There are books you can read and probably online refences on Sun's web site.
The implementation is generally a mark and sweep implementation on a low priority thread, and if you code simple experiments, you can see garbage collection in action, by overriding the finalize method and then callng System.gc after setting references on instances to null.
Regards,


Howard Kushner<br />IBM Certified Enterprise Developer - WebSphere Studio Application Developer V5.0<br />IBM Certified Advanced System Administrator - WebSphere Application Server V5.0<br />IBM Certified Solution Developer - Web Services with WebSphere Studio V5.1<br /><a href="http://www.amazon.com/exec/obidos/tg/detail/-/1931182108/" target="_blank" rel="nofollow">Developing J2EE Applications with WebSphere Studio</a> my Certification Study Guide for IBM Test 287
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need clarification on garbage collection