Solved

Garbage Collection in Java, Could you please make “corrections” and provide “clear” explanations for my HEAP/STACK diagram?

Posted on 2011-02-26
8
771 Views
Last Modified: 2012-05-11
Question:
This is a question about garbage collection. I want to construct a HEAP/STACK diagram for the code given below to determine the number of Objects created, and number of Objects eligible for garbage collection.

I have already drawn the HEAP/STACK diagram, which is not giving me the right answer . The locations assigned for each objects are randomly given e.g. 100,200,300.
Could you please make “corrections” and provide “clear” explanations for the HEAP/STACK diagram?

Thank you in advance :)


Java Code Given:

class Dozens
{
   int [] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
}

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

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


My Diagram:
HEAP
100: Dozens referenceBy da[]
200: Dozens referenceBy da[0]
300: IntegerArrayObject referenceBy 200
400: Dozens referenceBy d                   (GC Eligible)
500: IntegerArrayObject referenceBy 400      (GC Eligible)
600: Dozens referenceBy da[1]                  (GC Eligible)
700: IntegerArrayObject referenceBy 600      (GC Eligible)

STACK
da[] = 100
da[0] = 200
d = 400 (NULL)
da[1] = 600 (NULL)

Correct Answer: C & F from options
My Answer (Wrong): 7 Objects are created, and 4 Objects are eligible for Garbage Collection
0
Comment
Question by:techbro
  • 3
  • 3
  • 2
8 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 34993231
Is this homework?

The answer is c and f becaus there are only objects created when there is a new.  All you need to do is count the NEW's.

As for the object available for gc you count the NULL's, then for every object set to NULL count the 'new'ed objects inside the object. Since these objects don't allocate anything it is the same as the number of NULL's.
0
 

Author Comment

by:techbro
ID: 34994208
Hi MogalManic,

I really appreciate your response.
Since you mentioned that the "Objects" are only created when there is a "new".I just want to know how "new Dozens[3]" &  "new Dozens()" are different?

I know that:
new Dozens[3] is an Array Object which stores references of 3 Dozen Objects
new Dozens() is only one Object

When new Dozen() Object is created, another "Array Object", int [] dz, is created as well. So when  new Dozens[3] Object is constructed, why the the "Array Object", int [] dz, is not created?

This is not a homework. I am studying for a certification exam - "Oracle Certified Professional, Java SE 6 Programmer". I approached this GC problem in the book written by Sierra & Bates. GC concept is a big setback for me to score my exam, so I want to eliminate all my doubts.

Thank you again,
Morshed
0
 
LVL 5

Assisted Solution

by:jaiminpsoni
jaiminpsoni earned 150 total points
ID: 34994913
When you create new Dozens[3], you just create an array object which contains 3 references.

It doesnt create the 3 Dozens object.

If you want to create those objects, you have to create those using new....

da[0] = new Dozens()
da[1] = new Dozens()


So, when you create array object, it just creates the array of references. Those references are null till you create new objects using new.

Hope this answers your question.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 34996506
jaiminpsoni is right!  I got caught in the "trick" question.  An array is a pointer to an "object", that contains multiple indexes.   Each index needs to be initialized either using static initializers ({}) or manually for each index ([]).

The two NEW's allocate on the heap, but also all of the array allocations are heap allocations (with or without the new).  Since there are three arrays (one in main and 2 in each instance of Dozens), the total of allocations is 5

As for the GC, when the items are set to NULL, it is the SAME object (d and da[1] refer to the same object).  So one Dozens object is "freed" and it's allocated array is "freed".
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 21

Accepted Solution

by:
MogalManic earned 350 total points
ID: 34996611
Here is a before and after picture of the heap and stack:
Clipboard01.png
Clipboard01.png
0
 

Author Comment

by:techbro
ID: 35003162
Thanks MogalManic and jaiminpsoni for your prompt response!
The concept of array allocated in heap makes sense to me.

I have one more question regarding garbage collecting an array.
Since MogalManic mentioned that da[] is pointing to a location, "100". Is it possible to make the entire Dozen Array be eligible for GC? How can I point the da[] to null instead of 100?


I know this is a syntax error and never compile if I try da[] = null.
0
 
LVL 5

Expert Comment

by:jaiminpsoni
ID: 35003925
you need to do
da = null;
0
 

Author Comment

by:techbro
ID: 35003935
Thanks jaiminpsoni!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
Java Message handling in Service Layer 3 55
Unable to start eclipse ? 17 127
Checkbox and ListView in Android Layout 4 56
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now