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
776 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
Unhandled exception type Exception 18 43
Math Equation 23 106
What's wrong with this code? 4 28
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

820 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