• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Garbage collection

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object references passed as a part of the method are part of Stack
or Heap?
When they are eligible for garbage collection?

K&B book chapter 3, Qno 2:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
266 Chapter 3: Assignments
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.


here cb is part of Heap or stack?
When we say garbage collection its applicable for Heap only.
Need clarification.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object itself is on the heap. When passed as a method's formal parameter, the reference variable that points to the object will be on the stack.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here i think cb is part of stack as all the arguments and variables that are needed by a method for its execution are stored in the function stack and each function has a stack , whenever a function is called the current context(the registers it had been using, the variables being used , the IO devices being used, the content of the program counter(PC) etc etc) of the calling program is moved form the main memory and is placed in one of the process registers and then the context of the called function is placed on the stack . when the function terminates the stack is emptied and is agin loaded with the context of the calling program as a result the execution resumes from line next from which the function was called


secondly in this question cb is not an object but a reference . garbage collection is only applicable to objects and not to references and as objects are placed on heap so it is said that garbage collection its applicable for Heap only i hope it clears your doubt
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shaily Sharma:
Object references passed as a part of the method are part of Stack
or Heap?
When they are eligible for garbage collection?

K&B book chapter 3, Qno 2:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
266 Chapter 3: Assignments
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.


here cb is part of Heap or stack?
When we say garbage collection its applicable for Heap only.
Need clarification.



Reference variables are never grbage collected, instead objects from are garbage collected. A reference variable only refers to n object on heap.

Now coming to your example posted above let me give you a hint, when you pass a reference as part of method then you pass that reference as a value to a method.

Hope this clarifiction would help you.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Actually all the objects will be on a heap. Object references are not objects at all. So neither of c1, c2, c3 and cb will be on the heap. They all will be on the stack only.

Now, c1 will be pointing to a new object object1 of type Cardboard and c2 will be pointing to a new object object2 of type Cardboard. Remember that these are only references. So when we pass c2 as an argument, we are actually assigning a new reference to the already available object (pointed to by c2). So cb also now points to object2. Within the function, this object reference cb is set to null and the null object is returned. This is assigned to c3.

Note that the connection of cb with object2 has been cut, but still object2 has a reference c2 pointing to it.

Now c1 is set to null. So, object1 is eligible now for garbage collection. Totally, the references c1, cb and c3 point to no objects. Object1 has no reference now. Object2 has c2 pointing to it.

So, only one object is eligible for garbage collection.

Get back in case of any more doubts...

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

First off, welcome to the ranch! We've found that things are a lot friendlier when people use their first and last names as their display names, so I'd ask you to update your display name to match our policy - thanks!

Second - your answer is almost correct, but not quite...can you spot what you missed?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Short object which seems to have not-gathering-the-attention is also eligible here. Right Bert?

This quesiton was dicussed here but right now i dont have the link to those threads!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic