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
768 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

759 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

22 Experts available now in Live!

Get 1:1 Help Now