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 HI please Help with. GC Kathy and Bert Book 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 "HI please Help with. GC Kathy and Bert Book" Watch "HI please Help with. GC Kathy and Bert Book" New topic
Author

HI please Help with. GC Kathy and Bert Book

Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Hi Folks. i have a question about GC.

here it´s the question at book chapter 3 problem 10.

Which two are true about the objects created within main(), and eligible for garbage collection
when line 14 is reached?
A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One object is eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC

correct answer is C and F.

i am ok with the answers but i have 4 questions.

1). here d = null; i am deleting a reference. but da[1] it´s still alive?? if this line da[1] = null; was comment 0 elements would be elegible??
2). class Dozens{int a[];} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2?
3). Dozens a[] = new Dozens[3]; i am creating just one object here.
4). class Dozens{String Name = new String("MyName");} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? how many objects may be elegible for
GC the one on the heap??

i know the question is very straightforward but a bunch of people but GC it's the topic more difficult to me so far.

Thanks a Lot.

Sorry by my poor english.

Amir Ashfaq
Greenhorn

Joined: Feb 25, 2011
Posts: 13

Ok i will first try to explain how many Objects are created...

As execution starts from MAIN....

line 05: One Object of type "Array" is Created.
line 06: Two Objects are created.. ONE: of type Dozen .. TWO: array type referenced by"dz".
line 07: Same 2 Objects (Dozen , Array ) .


/// so total 5 objects are created.

line 08: array's second index will point to same object as "d".
line 09: d is null but array's secound index is still pointing to that object.
line 10: now array's secound index is null...so that object of type Dozen and one array object referenced by local "dz" variable, both are eligible for garbage collection.


feel free to ask again if still have any issue...
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Amir Ashfaq wrote:
I Understand very clear Amir But what about my other 2 questions namely
2). class Dozens{int a[];} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? without instance a explicity array a.
4). class Dozens{String Name = new String("MyName");} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? how many objects may be elegible for
GC the one on the heap??
Amir Ashfaq
Greenhorn

Joined: Feb 25, 2011
Posts: 13

First of all , when object is created???

int a[]; // just a reference type variable. "a" .. which can point to an array. SO no object is created until array is initialized...

like when you write

int a[] = new int[3]; or int a[] = new int[] {1,2,3};
then one array type OBJECT is created.... that object will have 3 indexes capably of storing int type variable each.


so Question 1: there you have one reference variable of type int array, not object. and one "a" reference variable actually pointing to an Object of type Dozens. SO here we have only 1 Object.


Question 2: when you use new keyword to create String Object, the object actually is on heap and the valse "MyName" is stored on pool (as far as i know about it....).

and same for one object of type Dozens... so here we have 2 objects (string, Dozens)
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Amir Ashfaq wrote:Awesome amir i understand very clear now. thanks a lot.
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
Hi Amir,

This is my first post on java forums as I am preparing for SCJP certification. I have a question on your expalnation for the follwoing lines...

line 06: da[0] = new Dozens();
line :07 Dozens d = new Dozens();

You said
line 06: Two Objects are created.. ONE: of type Dozen .. TWO: array type referenced by"dz".
line 07: Same 2 Objects (Dozen , Array ) .

On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.
Amir Ashfaq
Greenhorn

Joined: Feb 25, 2011
Posts: 13

ok....

new Dozens() creates object of type Dozen, but you need to look in the Dozen Object... it has class variable "dz", which is integer type array. so when one Dozen object is created then one array "dz" is also created as its properly initialized. right?

So when array is created, on heap one Object of type array is created. That object have references to the indexes capable of storing array elements(in this case INTs).


So thats why 2 objects on heap...one Dozens and one Array,

Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

Christian Daniel Ortiz Cuellar wrote:i know the question is very straightforward but a bunch of people but GC it's the topic more difficult to me so far.

Whenever solving the GC problems try to solve by drawing cloud diagrams with your own conventions rather doing mental calculation,
it will be very easy and saves time too.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Christian Daniel Ortiz Cuellar wrote:Hi Folks. i have a question about GC.

here it´s the question at book chapter 3 problem 10.

Which two are true about the objects created within main(), and eligible for garbage collection
when line 14 is reached?
A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One object is eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC

correct answer is C and F.

i am ok with the answers but i have 4 questions.

1). here d = null; i am deleting a reference. but da[1] it´s still alive?? if this line da[1] = null; was comment 0 elements would be elegible??
2). class Dozens{int a[];} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2?
3). Dozens a[] = new Dozens[3]; i am creating just one object here.
4). class Dozens{String Name = new String("MyName");} Dozens a = new Dozens(); who many objects i am creating here. 1 or 2? how many objects may be elegible for
GC the one on the heap??

i know the question is very straightforward but a bunch of people but GC it's the topic more difficult to me so far.

Thanks a Lot.

Sorry by my poor english.



Hola Daniel! You know I Love Spanish Language!...I Have A Very Useful Advise For You...I Had The Same Problems As You With Garbage Collection And I Decided To Use The System Of Drawing A Sketch Map Of The 'Heap' (Remember That The Heap Is Where Objects Are Stored In Memory) .

1. READ THE QUESTION VERY CAREFULLY

2.READ THE QUESTION AGAIN AND AS YOU READ START DRAWING:

a. THE 'HEAP' AND OBJECTS CREATED IN THE HEAP
b. REFERENCE VARIABLE THAT POINTS TO OBJECTS IN THE HEAP

HINT:
a. CONSIDER THAT THE REFERENCE VARIABLES ARE IN 'THE-OUTSIDE-WORLD' REFERING TO OBJECTS ON THE 'HEAP' (INSIDE-WORLD)
b. OBJECTS THAT CANNOT BE REFERENCED FROM THE OUTSIDE WORLD ARE ELIGIBLE FOR GARBAGE COLLECTION.

PRACTICE DRAWING EVERYDAY TILL YOU ARE NEARLY PERFECT
I DID THE SAME AND NOW GARBAGE COLLECTION IS 'ALMOST' LIKE A 'PIECE-OF-CAKE' FOR ME.

I HOPE THIS HELPS...MUCHAS GRACIAS.


[Thumbnail for The_Heap.png]


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
hi Jackzia,

creating our own diagram for this GC helps a lot...your way of picturization helps a lot and gave better understanding on the creation of object and when its availbale for GC.

here is one more question related to GC....can you explain with the diagram..how many objects are created and how many objects are eligible for GC at this comment //line 1
I drew a picture of this with 3 objects on the heap....but couldn't get how I can connect A aob reference variable to link other objects(A a = new A(); A b = new A(); A c = new A();)


class A {
A aob;

public static void main(String args[]) {
A a = new A();
A b = new A();
A c = new A();
a.aob = b;
b.aob = a;
c.aob = a.aob;
A d = new A().aob = new A();
c = b;
c.aob = null;// line1
System.gc();
}
}


Thanks,
Lakshmi
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Ikpefua Jacob-Obinyan wrote:
perfecto asi se hace.
hey jacob. i am still have small problems with GC but i think i am growing a little bit.
take a look at this. this is from whizlabs how many objects are elegible for GC after line 11. i think syntax is wrong missing return type but.

options
1,0,2,3

i think only one object is ready for GC which is "i" i am correct or any ideas.

other question beside. Strings i heard that integer and char also has some kind of pooling.

example.
Integer a = 127;
Integer b = 127;
System.out.println(a==b); //true
but this is also works for new Integer("127"); i am creating one on pool and one on heap.???
by the way mi correo es chiperortiz@hotmail.com thanks a lot
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
also on the first quesion instead of int [] array (int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};} ) if it has some other primitive variable (like just int or float..) then there could be 3 object created and one is eligible for garbage colleciton ..is that correct??

I assume just int i = 5; doens't create the object on teh heap??? any suggestions??
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Lakshmi Yerr wrote:also on the first quesion instead of int [] array (int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};} ) if it has some other primitive variable (like just int or float..) then there could be 3 object created and one is eligible for garbage colleciton ..is that correct??

I assume just int i = 5; doens't create the object on teh heap??? any suggestions??


a more interesting question would be..

a array of Objects.

Integer A[] = {1,2,3,,4,5}; how many objects i am creating here.??
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
my understanding is you can't create Integer A[] = {1,2,3,,4,5}; its a type mismatch can't convert from int to Integer...
objects are created if you use either "new" or instantiate teh array (if it is an array)

So my answer would be there are no objects created for Integer A[] = {1,2,3,,4,5};
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Lakshmi Yerr wrote:my understanding is you can't create Integer A[] = {1,2,3,,4,5}; its a type mismatch can't convert from int to Integer...
objects are created if you use either "new" or instantiate teh array (if it is an array)

So my answer would be there are no objects created for Integer A[] = {1,2,3,,4,5};


int is implicit boxed to Integer. this Integer A[] = {1,2,3,,4,5}; is perfectly legal.. as JDK 6. ilegal before JDK 6.

my question might be..

A[] is a Object because array extends Object. but in each position is as well a object. my question is how many objects is created here.

sorry if i made a wrong appointment.

Christian Daniel Ortiz Cuellar
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
ok got you ..it compiles with java 6 ......one object created for Integer A[] = {1,2,3,,4,5}; because intialization happens.......if its just Integer a[] then no object is created on teh heap.. ..is that correct?
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Lakshmi Yerr wrote:ok got you ..it compiles with java 6 ......one object created for Integer A[] = {1,2,3,,4,5}; because intialization happens.......if its just Integer a[] then no object is created on teh heap.. ..is that correct?


hi Lakshmi as long as i know about GC (not much).

A[]; just a variable reference not object is create..

A = {1,2,3,4,5} // one object is created.

A = new int[]{1,2,3,4,5} // one object is created as well.. i am not sure about this. but i think a object is created.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Lakshmi Yerr wrote:Hi Amir,

This is my first post on java forums as I am preparing for SCJP certification. I have a question on your expalnation for the follwoing lines...

line 06: da[0] = new Dozens();
line :07 Dozens d = new Dozens();

You said
line 06: Two Objects are created.. ONE: of type Dozen .. TWO: array type referenced by"dz".
line 07: Same 2 Objects (Dozen , Array ) .

On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.

On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.

of course one of type Dozens (class) and other attach to it Short Object...
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Lakshmi Yerr wrote:hi Jackzia,

creating our own diagram for this GC helps a lot...your way of picturization helps a lot and gave better understanding on the creation of object and when its availbale for GC.

here is one more question related to GC....can you explain with the diagram..how many objects are created and how many objects are eligible for GC at this comment //line 1
I drew a picture of this with 3 objects on the heap....but couldn't get how I can connect A aob reference variable to link other objects(A a = new A(); A b = new A(); A c = new A();)


class A {
A aob;

public static void main(String args[]) {
A a = new A();
A b = new A();
A c = new A();
a.aob = b;
b.aob = a;
c.aob = a.aob;
A d = new A().aob = new A();
c = b;
c.aob = null;// line1
System.gc();
}
}


Thanks,
Lakshmi


HINTS:
- REMEMBER THAT class A Has-a REFERENCE VARIABLE aob.
- REMEMBER THAT EVERY INSTANCE OF class A Has-a (ITS OWN) REFERENCE VARIABLE aob.
- A REFERENCE VARIABLE (to the best of my knowledge) CANNOT REFER 'DIRECTLY' TO TWO OBJECTS AT THE SAME TIME
- TWO REFERENCE VARIABLES (to the best of my knowledge) CAN REFER TO ONE OBJECT AT THE SAME TIME

VERY IMPORTANT! THOSE OF YOU THAT STUDY WITH K&B BOOK, GO TO CHAPTER 3 AND SPEND A WHOLE DAY
-IF POSSIBLE- TO READ AND UNDERSTAND 'EVERY-DETAIL' OF PAGES 184 & 185, IF YOU HAVE ANY DOUBTS,
BRING IT HERE AND LET US TRY TO PUT OUR HEADS TOGETHER AND RESOLVE THE DOUBT.
priyadharshini vijayaraghavan
Greenhorn

Joined: Mar 11, 2011
Posts: 21
Hi,

In the above program, objects eligible for GC will be 2.

The one referenced by c and an another object new A().

the
A d= new A().aob=new A();
In this line d is set to the assignment of (new A().aob) which inturn is set to a new Object new A ();
so the first object """new A().aob"" will be eligible for GC.


go to post "GC program needs Explanation"... The explanation to the same problem is done there.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

priyadharshini vijayaraghavan wrote:Hi,

In the above program, objects eligible for GC will be 2.

The one referenced by c and an another object new A().

the
A d= new A().aob=new A();
In this line d is set to the assignment of (new A().aob) which inturn is set to a new Object new A ();
so the first object """new A().aob"" will be eligible for GC.


go to post "GC program needs Explanation"... The explanation to the same problem is done there.


priyadharshini Yes!! You Are VERY VERY Correct!!... I Will Make The Necessary Adjustments To The Diagram!!.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Ikpefua Jacob-Obinyan wrote:
Lakshmi Yerr wrote:hi Jackzia,

creating our own diagram for this GC helps a lot...your way of picturization helps a lot and gave better understanding on the creation of object and when its availbale for GC.

here is one more question related to GC....can you explain with the diagram..how many objects are created and how many objects are eligible for GC at this comment //line 1
I drew a picture of this with 3 objects on the heap....but couldn't get how I can connect A aob reference variable to link other objects(A a = new A(); A b = new A(); A c = new A();)


class A {
A aob;

public static void main(String args[]) {
A a = new A();
A b = new A();
A c = new A();
a.aob = b;
b.aob = a;
c.aob = a.aob;
A d = new A().aob = new A();
c = b;
c.aob = null;// line1
System.gc();
}
}


Thanks,
Lakshmi


HINTS:
- REMEMBER THAT class A Has-a REFERENCE VARIABLE aob.
- REMEMBER THAT EVERY INSTANCE OF class A Has-a (ITS OWN) REFERENCE VARIABLE aob.
- A REFERENCE VARIABLE (to the best of my knowledge) CANNOT REFER 'DIRECTLY' TO TWO OBJECTS AT THE SAME TIME
- TWO REFERENCE VARIABLES (to the best of my knowledge) CAN REFER TO ONE OBJECT AT THE SAME TIME

VERY IMPORTANT! THOSE OF YOU THAT STUDY WITH K&B BOOK, GO TO CHAPTER 3 AND SPEND A WHOLE DAY
-IF POSSIBLE- TO READ AND UNDERSTAND 'EVERY-DETAIL' OF PAGES 184 & 185, IF YOU HAVE ANY DOUBTS,
BRING IT HERE AND LET US TRY TO PUT OUR HEADS TOGETHER AND RESOLVE THE DOUBT.

-THE DIAGRAM CLEARLY SHOWS THAT OBJECTS INICIALLY REFERED TO BY REFERENCE VARIABLES c AND d ARE ELIGIBLE
FOR GARBAGE COLLECTION


[Thumbnail for The_Heap_aob.png]

Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
hi,
I understand how c i seligible for gor GC, but not clear hoe d is eligible for GC... d is first assinged to one object and that object is assigned to new a()..then how d is eligible for GC???.....thanks in advance for all your explanation on this so for...
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
hi,
I understand how c is eligible for gor GC, but not clear how d is eligible for GC... d is first assinged to one object and that object is assigned to new a()..then how d is eligible for GC???.....thanks in advance for all your explanation on this so for...
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

@Lakhsmi,
Welcome to JR. Whenever you have a new question, feel free to create a new thread.
and please use code tags.
Christian Daniel Ortiz Cuellar wrote:i heard that integer and char also has some kind of pooling.

Yeah,you heard true.Integers,Shorts,Bytes and Chars when their wrapped primitive value lies between -128 to 127, they too have pooling mechanism similar to Strings.
Lakshmi wrote:also on the first quesion instead of int [] array (int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};} ) if it has some other primitive variable (like just int or float..) then there could be 3 object created and one is eligible for garbage colleciton ..is that correct??

if we replace with .then as you said 3 objects were created and one is eligible for GC.
Christian wrote:int is implicit boxed to Integer. this Integer A[] = {1,2,3,,4,5}; is perfectly legal.. as JDK 6. ilegal before JDK 6.

Remember , auto boxing is introduced in java 5. so it is illegal before jdk 5.
Christian wrote:On line 6 how its going to create two objects? There is only one "new" which means only one obect is created...same thing on the next line. Appreciate your help on this.

Yes, there is a single new ,but note that it got some instance variables, they are initialized when ever a new object of Dozens is created.if array variable would have been just a reference instead of initializing in that case it is initialized to null.
Lakhmi wrote:I understand how c i seligible for gor GC, but not clear hoe d is eligible for GC... d is first assinged to one object and that object is assigned to new a()..then how d is eligible for GC???

no object referenced by d is not eligible for GC.it is the anonymous object created in the chain assignment.Please go through this post.

HTH
 
 
subject: HI please Help with. GC Kathy and Bert Book
 
Similar Threads
Garbage Colection
K&B SCJP6 Chapter 3- Q10 a GC question
Help,A GC problem
Garbage Collection - P274 Kathy Sierra
K&B - Chapter 3 - Self test 10 - GC