wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

bobby chaurasia
Ranch Hand

Joined: Mar 30, 2002
Posts: 84
For the following question...
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1 = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}

After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

I selected e which is correct. My reasoning was,
when method m1 is called, a copy of the array a1 is passed and set to null. But the original array a1 is still not null so its not eligible for garbage collection.
Is this line of reasoning okay ?
Thanks
Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi ,..
its not a copy of Array is passed to method , rather a copy of reference is passed.Therefore, when you set null to that reference in method,only that reference is not pointing anywhere but, still another reference will be there in the main method which is pointing to array location.
Hope i am clear.
------------
Prashant


--------------<br />Prashant<br />SCJP-91%
Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
another thing bobby ,
there is no original array and previous array. There will be only one Array and we will be having different references for that Array.
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
Prashant Neginahal,SORRY,it is not clear,can you tell it in a simple manner? what is GARBAGE COLLECTION mean?? thanks!
[ January 08, 2003: Message edited by: Melliholic Michael ]

a beginner in java
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Melliholic -
In a tiny, tiny nutshell... Garbage Collection refers to an automatic process that the JVM provides. Periodically, the JVM will start a process known as Garbage Collection. That process looks through the heap (that part of memory where ALL objects are stored), and removes objects that can't be reached by any live thread. This process frees up memory for your Java application to re-use if needed.
A few key points:
- You can ask the JVM to run garbage collection from within your Java program, but it's only a request - no guarantees!
- The GC ONLY works on objects - never touches variables in the stack - they have their own life and death process.
- If you can't refer to an object anymore in your program, it's eligible for GC. (say by setting the reference var. to null, or to a different object)
There are a million other posts and refences concerning the garbage collector on JavaRanch.
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
BTW -
It is correct to say that in this case there is only one array. However for a short time there are two reference variables referring to it. Nulling one of the references still leaves you with one valid reference to the array, so all of the objects are accessible and there is no garbage collector bait.
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
Bert Bates ,if I ask
**********************
I[] a1=new I[5];
I[] a2=a1;
a2=null;
**********************
is there any GarbageCollection??
thanks
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No it will not because the array object is still referenced by a1. I like to think of a1 and a2 as alias to the same object. This facilitates understanding.
However the question "will be any garbage collection?" is not ok. The g.c. will take place if the heap is full of objects (simply speaking) or sometimes hinted by System.gc(). Thus nullifying all the (active) references to an object will not provoke immediately (if ever) its g.c.ion. The question you will be asked is "made eligible for garbage collection?", meaning that if the g.c. would run then, the object would be g.c.ed


SCJP2. Please Indent your code using UBB Code
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
Jose Botella,when I run
a2=null;
will a2 be g.c.ed?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Melliholic -
If you go to the page 5 history on this forum, you will see a post I made on 12/18/02 - its title is 'One more time G.C. Q and A'
This post has diagrams showing a complex GC question - I think if you study it - it might answer your questions - if you study it and have more questions - let me know.
-Bert
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hi, I was reading a thread about G.C. and here was the code in it... (see below), and amyway the general consensus seemed to be that four objects were available for Garbafge Collection. But I thought only a reference was being set to null here?
class Garbage
{ public static void main (String[] args)
{ String[] str = new String[3];
Str[0] = new String("A");
Str[1] = new String("B");
Str[2] = new String("C");
str = null;
// Suppose the code compiles, then,
// How many objects are eligible for garbage collection, here at this line.
}
}


giddee up
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Jas -
You have to ask yourself this question...
Once str = null, how can you access the str array object or any of the three objects in that array??
If you can't reference an object it is eligible for the GC.
Does that make sense?

-Bert
also - see recent post 'one more time...'
Andy Peter
Greenhorn

Joined: Dec 26, 2002
Posts: 11
I am not sure whether any one answered Bobby or not, I think the answer e. is correct.
For the following question...
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1 = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

I selected e which is correct. My reasoning was,
when method m1 is called, a copy of the array a1 is passed and set to null. But the original array a1 is still not null so its not eligible for garbage collection.
Is this line of reasoning okay ?
Thanks
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Anand -
I thought we answered the original question
If not, here goes:
It is correct to say that no objects will be eligible for garbage collection. However...
Terminology is VERY IMPORTANT ! The exam wants to know how many objects are eligible for garbage collection, and it will try to trick you into thinking about reference variables.
In this case (the original question) there are NEVER two arrays!! There are however, for a brief moment two references variables that refer to the same array.
1 array
2 references to that array
Nulling one of the references doesn't make the array GC eligible because there is still a valid reference to the ONE AND ONLY array
- does that make sense?
-Bert
p.s. Please see recent post 'One more time GC....' it takes a similar test question example and shows you how to analyze these problems to get the right answer. It's amazing to me how much emphasis the Sun exam places on GC, but since they do, it's a pretty easy topic to understand, and once you do, you get a LOT of easy questions ! (the GC questions).
p.p.s. Somebody asked recently if you have to draw the little pictures to solve these problems, and I think that you do - for GC and for multi-dimensional array questions - Kathy and I have talked about it a lot, and for these questions a picture is worth 1000 words!
[ January 10, 2003: Message edited by: Bert Bates ]
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Thanks Bert, i am off to read the post "one more time" (er, no haven't read it yet, that's the name -what name -one more time -yes okay read it again but what's the name? -one more time )
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
Bert is of great help
Andy Peter
Greenhorn

Joined: Dec 26, 2002
Posts: 11
Bert,
my mistake, I misunderstood the original question to be asking what is the correct answer? instead Bobby was asking for the reasoning behind e. being the correct answer. You explained it very well.
Anand
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Garbage Collection
 
Similar Threads
garbage collection doubt
A question on Dan's GC Question
Garbage Collection II
Dan exam doubt 19
garbage collection