• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HI please Help with. GC Kathy and Bert Book

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 13
Mac Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
Mac Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amir Ashfaq wrote:Awesome amir i understand very clear now. thanks a lot.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
Mac Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
The_Heap.png
[Thumbnail for The_Heap.png]
 
Prasanna l reddy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

The_Heap_aob.png
[Thumbnail for The_Heap_aob.png]
 
Prasanna l reddy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
reply
    Bookmark Topic Watch Topic
  • New Topic