?
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
Medium Priority
?
779 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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