• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

RHE QUESTION - stack

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can use of the following class create a memory leak in a single-threaded application?
1. class MyStack {
2. private Object[] data = new Object[1000];
3. private int stackPtr = 0;
4.
5. public void push(Object pushMe) throws Exception {
6. if (stackPtr >= 1000)
7. throw new Exception();
8. data[stackPtr++] = pushMe;
9. }
10.
11. public Object pop() throws Exception {
12. if (stackPtr == 0)
13. throw new Exception();
14. return data[--stackPtr];
15. }
16. }
ANS:YES
Correct selection is: A
Suppose you push 1000 references to very large objects. You later pop 1000 times, so the stack is seemingly empty. Now suppose eventually all of your references to all 1000 large objects go out of scope; you would assume the 1000 large objects are garbage collected. However, each object is still referenced by an element in the stack's array, so garbage collection will never occur. To fix the class, pop() should set newly-vacated elements to null.
The pop method shd be llike this: I took this eg from pg 23 of RHe:
public Object pop() throws Exceptions
{
if (stackPtr == 0)
{
throw new Exception();
}
Object givevalue = data[stackPtr];// LINE 1
data[stackPtr ] = null;// LINE 2
return givevalue;
}
PROBLEM: MY QUESTION IS:
At line 2 I set data[stackPtr] to null, so it can be gced.
But at line 1 is not givevalue still pointing to data[stackPtr]?
hence i feel data[stackPtr] cannot be gced.
Please give me your comments as well as if this is an incorrect example , then give me a correct example.
Thanks in advance
Padmini

 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,
Any answers??
padmini
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if you create an instance variable of an object and if its reference is not set to null,will it result in memory leak???
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini

At line 2 I set data[stackPtr] to null, so it can be gced.
But at line 1 is not givevalue still pointing to data[stackPtr]?
hence i feel data[stackPtr] cannot be gced.


You are correct the givevalue object is referenceing the object that was just taken from the array. If you look at the explaination of the answer it says that when all of the references to the 1000 objects go out of scope. Meaning that after you return the reference from the pop() method you do something with it then set it to null or let it go out of scope then you might assume that the object has been garbage collected - not realizing that the array still references it.
Jeena
That will not always happen it is just possible for it to cause memory leak.

Hope this helps
Dave
[This message has been edited by Dave Vick (edited June 26, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Padmini,
You are getting a little confused over references and objects. The variable array data is just a reference to an object as is the local givevalue variable.
Garbage collection happens on objects not references. Therefore in the correct pop method what is happening is as follows:
givevalue --> object <-- data[stackPtr]<br /> Then we set data[stackPtr] to null we are left with:<br /> givevalue --> object
After we return from the method the givevalue scope is hit so the JVM sets it to null automatically. We are then left with:
object
which can be GCed because no references to it exist!
Regards,
Manfred.
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are creating an object as instance variable and not setting its reference to null,how can it be eligible for garbage collection???i think i am missing something here!!!
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini,
PROBLEM: MY QUESTION IS:
At line 2 I set data[stackPtr] to null, so it can be gced.
But at line 1 is not givevalue still pointing to data[stackPtr]?
hence i feel data[stackPtr] cannot be gced.
By setting data[stackPtr] to null,you haven't nullified the Object itself, just a reference to it that's no longer valid. The Stack object itself may have a long lifetime, and rewriting the pop method in this way helps ensure that garbage collection gets done in a timely fashion.

Rashmi.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Leonhardt:
Hi Padmini,
You are getting a little confused over references and objects. The variable array data is just a reference to an object as is the local givevalue variable.
Garbage collection happens on objects not references. Therefore in the correct pop method what is happening is as follows:
givevalue --> object <-- data[stackPtr]<br /> Then we set data[stackPtr] to null we are left with:<br /> givevalue --> object
After we return from the method the givevalue scope is hit so the JVM sets it to null automatically. We are then left with:
object
which can be GCed because no references to it exist!
Regards,
Manfred.


--------------------------------------------
Thanks manfred. I am clear now.
Thanks rashmi
Padmini
 
reply
    Bookmark Topic Watch Topic
  • New Topic