File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Guarenteeing G.C part Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Guarenteeing G.C part" Watch "Guarenteeing G.C part" New topic
Author

Guarenteeing G.C part

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
I am not getting g.c .Please help;
1)method1(){ Integer[] y = new Integer[20];}
---->here only one object
or 21 objects get eiligible for garbage collection at the end of the method?
2) void method2()
{
String s1 = "hello";
String s2 = "Pal";
s1 = s1+s2;// #5
}
Here after line #5 ,hello string get eligible for g.c, or never string
literals get garbage collected?
3)What is the meaning of resurrecting an object in finalize() method?
could you explain with an example?
4)What is this weak reference and all?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
1)method1(){ Integer[] y = new Integer[20];}
---->here only one object
or 21 objects get eiligible for garbage collection at the end of the method?

When you create an array, all components are initialized to their default values (here null), so there is only one object, that is the array reference by y, that will be gced.
2)
Here after line #5 ,hello string get eligible for g.c, or never string literals get garbage collected?

I'll let you search this forum to find an answer to this question.
3)What is the meaning of resurrecting an object in finalize() method?
could you explain with an example?

When an object is about to be garbage collected, its finalize method will be invoked. In this method, your code could easily reassign a reference to the current object to some reference variable somewhere which would have to effect of resurrecting the object since a new variable is referencing it, and thus, it is not unreferenced anymore.
Example:

Output:
Before GC :highlander
Before GC :mortal
Finalize invoked for "highlander". Save "highlander"
Finalize invoked for "mortal". Don't save "mortal"
After GC :highlander
4)What is this weak reference and all?
Actually you don't need to know that for the exam, but have a look at the following article which explains very well what WeakReference is and how garbage collection works:
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj
You can also consult the API:
http://java.sun.com/j2se/1.4/docs/api/java/lang/ref/WeakReference.html
[ March 21, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Hey Val, I had a question:
"1)method1(){ Integer[] y = new Integer[20];}
---->here only one object
or 21 objects get eiligible for garbage collection at the end of the method?
When you create an array, all components are initialized to their default values (here null), so there is only one object, that is the array reference by y, that will be gced."

When you said all components are initialized to their default values, didnt you mean that there will be 20 Integer object initialized to "null". And if so, don't they become eligible for GC? After all my understanding was that any null object is eligible for GC.
Thanks.


Anup Engineer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Pay attention to the terminology!!
You can set a reference to null but not an object.
When you create the array, all components are initializaed to null, this means that all references contained within the array have a null value, that is, do not reference any object. No object have been created yet, so no object is eligible for gc.
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
Originally posted by Valentin Crettaz:

You can set a reference to null but not an object.

Hi Valentin, can you show example about Refence and Object?
Thanks in advance!

--------------------------
class Obj
{
public static void main(String[] args)
{
Obj o = new Obj();
//in here ,o is object and Ojb is Refence

}
}
---------------------------
[ March 21, 2002: Message edited by: WiLL Tao ]

Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
for instance in the following statement
Vector v = new Vector();
v is a variable of type Vector that references an object of type Vector.
In
Object o = new Vector();
o is a variable of type Object that references an object of type Vector.
If you want you can identify a reference variable with a pointer to an object.
Now, say we have:
Vector v1 = new Vector();
Vector v2 = v1;
Then we have one object of type Vector that is referenced by 2 variables of type Vector. There is only one object. The following schema should make this clear:

You can see that we have two reference variables (or pointers) to the same Vector object. By invoking a method of the Vector class on the reference variable v1, you will "mutate" (change) the Vector object and the change is going to be visible through the reference variable v2. For instance:
Vector v1 = new Vector();
Vector v2 = v1;
v1.add("hello");
System.out.println(v2.get(0));
is going to output the String "hello" because v1 and v2 reference the same Vector object.
[ March 21, 2002: Message edited by: Valentin Crettaz ]
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
Many thanks for your teaching.
You are always kind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Guarenteeing G.C part