• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert Bates ,if I ask
**********************
I[] a1=new I[5];
I[] a2=a1;
a2=null;
**********************
is there any GarbageCollection??
thanks
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose Botella,when I run
a2=null;
will a2 be g.c.ed?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
}
}
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...'
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert is of great help
 
Andy Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic