• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Number of Objects created

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: KB Book

class Dozens
{
int[] dz={1,2,3,3};// object 1
}


public class Eggs
{
public static void main(String[] args)
{
Dozens[] da=new Dozens[3];//object 2
da[0]=new Dozens();//object 3
Dozens d=new Dozens();//object 4
da[1]=d;
d=null;
da[1]=null;
}
}

The answer is 5 objects are created and 2 eligible for GC. But, my answer is 4 objects created and 1 for GC.
[ November 16, 2008: Message edited by: Abhi vijay ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think your code is not complete...

> da[1]=d;//object 4

d? what type is d and were have you declare it?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So the answer is 5 objects created and 2 eligible for GC. Now you ask "why do you said 2 objects eligile for GC and spoken only about one Dozen object?

Have you ever heard about the "Island of Isolation"? Its an unreachable place for people outside the island, but inside the island thing happens. What do I mean? Look:

Before:



da[1] = null

After:



When you lose the reference to the Dozen object the int array (another object) is unreachable! It lives now in the Island of Isolation, the object itself still have a reference (the Dozen dz attribute) but since Dozen is unreachable you cant ever reach Dozen.dz!

Thats why you have 2 eligible objects for GC, not only the objects that have no reference become eligible, but all the objects that cant be reachable (living in the island of isolation).

So be carefull, when you find an object that has no reference, check if the object attributes doesnt refer to another objects that become unreachable too!

Sorry for the long post, hope it helps you understand what happen!

[ November 16, 2008: Message edited by: Fabio Nascimento ]
[ November 16, 2008: Message edited by: Fabio Nascimento ]
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The answer is 5 objects are created and 2 eligible for GC. But, my answer is 4 objects created and 1 for GC.



Maybe an option here is ... instead of everyone explaining why the answer is 5 objects created and 2 eligible for gc... it may be a good idea for you to explain why you think the answer is 4 objects created and 1 eligible for gc. This way we can figure out where you went wrong.

Henry
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now. I did not consider that every object of Dozens contains an object of int array.
Thanks a lot guys.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Statement #1: Dozens[] da=new Dozens[3];//object 2 Will create a reference name "da" in the main method stack and a new Array object in the Heap which can hold three objects of type Dozens.
Hence this statement will have one object of type Array on the heap which is refered by da.

Statement #2: da[0]=new Dozens();//object 3 : Will create a new Dozens object in heap and assigned to da[0]. Before the Dozens() constructor runs the member variable of Dozens dz will result in a new Array object creation. Hence after this statement completes there will be two more objects in the heap , first of type Dozens assigned to da[0] and a new array object assigned to dz memeber variable of da[0]. Hence every new Dozens object will result in two new object on heap.

Hence there are a total of 3 objects in the heap.

Statement# 3: Dozens d=new Dozens();//object 4 Will result in two more objects as explained above.

Hence there are a total of 5 objects in the heap.

da[1]=d; Will cause the second element of da array to refer the above object created by d.

Still there are a total of 5 objects in the heap.

d=null; Will cause the reference d to point to null but the object which it pointer earlier can still be refered from da[1] . Hence Still there are a total of 5 objects in the heap.

Still none of the objects are eligible for garbage collection.

da[1]=null; Will cause the object created by statement #3 no longer accessible from any live thread and hence that object will be available for GC. Since its a Dozens object it will cause dz also to be available for GC because although dz can be accessed via its object but the object itself cannot be accessed from live thread.

Hence a total of 5 objects are created and 2 objects are eligible for GC.
Note "da" & "d" are local variables in the main method stack and not objects in HEAP.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,

I have a question here.
According to SCJP 1.5 Exam study guide (by Kathy Sierra & Bert Bates).
If we want to make an object eligible for Garbage collection we can reference the object to null.

According to the example stated in the above posts , the object d=null should also be eligible for garbage collection.

-Jyothsna
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic