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

Garbage collection

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was going through previous postings when i got this one
How many objects are eligible for GC
String name;
String newName = new String("Nick");
newName = new String("Jason");
name = new String("Frieda");
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
I say 1 (b).
Isnt the answer (c) ie 2 objects are eligible for GC since both
Nick and Jonshon are not referenced.
Please clarify.

------------------
Regards,
V. Kishan Kumar
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i think you are right. only "Nick"is eligible for gc, while "Jason" is still holding by newName and "Frieda" is holding by newestName variable.
rong chen
[This message has been edited by Rong Chen (edited October 05, 2000).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys ...
Think there are 2 objects eligible for collection:
The String object "Nick", which is no longer being referenced.
The String object reference 'name' which was set to 'null'.


------------------
Jane
 
Rong Chen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Sorry, but don't think so:
>name = new String("Frieda");
>String newestName = name;
>name = null;
"Frieda" is not eligible for GC yet since newestName is still holding its reference. Yes, name is assigned to null, so if the object is only referenced by name, then it is eligible for GC. but we get another variable holding the referece of "Frieda", the newestName is not null, right? the value of newestName is the reference to "Frieda", thus "Frieda" is NOT eligible for GC.
Could anyone correct me if I comment wrong?
regards
rong chen
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.>String name;
2.>String newName = new String("Nick");
3.>newName = new String("Jason");
4.>name = new String("Frieda");
5.>String newestName = name;
6.>name = null;
There is only one object (Nick) eligible for GC. "Frieda" is referenced by name. newestName has a copy of that reference.
name ---> Frieda <--- newestName
Even if name is set to null, "Frieda" is still hold by newestName.

------------------

[This message has been edited by Adrian Yan (edited October 05, 2000).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ...
Hate to beat a dead horse and maybe I've got this all wrong but ... aren't <code>name, newName </code> and <code>newestName</code> all Objects and "Frieda", "Nick", "Jason" are seperate String objects?
That means we're dealing with 6 different objects.
The object reference 'newName' is pointing to 'Jason', so neither are eligible for gc.
The object reference 'newestName' is pointing to 'Frieda', so neither are eligble for collection.
The object reference 'name' is set to null; so it's eligible for gc.
The String object 'Nick' is no longer being referenced, so it's eligible for collection.
Did I miss something?

------------------
Jane
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,

aren't name, newName and newestName all Objects and "Frieda", "Nick", "Jason" are seperate String objects?


"Frieda" is a constant whose address is being held by name and newestName until name is set to null. "Nick" and "Jason" are also constants. The String is the object in each case and the variable is the object reference.
Marilyn

[This message has been edited by Marilyn deQueiroz (edited October 07, 2000).]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Friends,
Please check out the following link, I hope it will put some light on the topic.................
http://www.javaranch.com/maha/Discussions/Garbage_Collection/GC_question___________-_JavaRanch_Big_Moose_Saloon.htm

All the Best
Manish
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manish for the link,
I think there is a lot to pick and learn from Maha, Steve and Jims disscussion. Its really not an easy concept and one has to have one's mind clear about it before going for the exam.
Thanks again
Nasir
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jane,
An Object reference is not the same as the object itself. if you just declare lots of object references, and assign them to null, you still havent created a single object.
I am sure that memory is assigned to store the identifiers of object references, but that is done internally by the jvm. We cannot treat Object reference Identifiers as Objects.More specifically the Identifiers can refer to objects, and have a type associated with them, but in itself, an Identifier is not an object.
I hope I am clear.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Marilyn and Mohit.
I read maha's discussions on Strings and now realize I'm guilty of using 'loose language' in referring to 'object reference variables' as objects.
Still have one doubt though, as a variable also takes up memory wouldn't it be eligible for gc when it no longer points to an object?
------------------
Jane
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jane,
I suppose I am not the right person to answer that question. Experts in JVM are required.
However my guess is that identifiers are part of program itself.
So when a program is loaded into memory, the identifiers are also loaded along with it, just like method tables etc. So I suppose, identifiers are not eligible for garbage collection.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object references are just named addresses. They are nothing but address of some memory location where the actual object is stored. The actual object usually resides in the program memory ie., heap and is accessed as needed.
Imagine a two-column table with the different names in the first column ( EmployeeName, whateverId, someObject etc ) and addresses in the second column- just raw hexadecimal numbers ( 0x2322, 0x8977 etc ) for each name in the first column. This is roughly how JVM stores the references. Whenever the program tries to access an object, its name is looked up in the first column, its address fetched from the second column of the appropriate row and then the JVM 'seeks' the addressed referred to by the big-long hexadecimal number. If anything meaningful found there ie., if it finds what it is looking for, then the reference is said to have been meaningfully resolved. Else you will get a runtime error - for example NullPointerException is thrown if nothing is found in that memory bin.
Remember, declaring object references is really not as expensive as creating objects[/i]. This is true because object references are nothing but integer/long data types which store an address instead of a primitive value. You can declare any number of references without being worried about the cost or the overhead. However when you create an object, you are consuming memory. There is only so much memory available to your( which is not infinite by the way ) program and JVM tries hard to make sure you don't run out of the quota. Every time you create an object using <code>new SomeObject()</code> the amount of memory available for your program decreases. The object may occupy only a fraction of the available memory, but irrespective of "how much" space an object eats up, the Garbage collector needs to keep track of its "active references" and strive to reclaim the memory when the object is no longer being used.
Moral of the story is - unlike objects primitives aren't considered entities that eatup valuable memory resources. It doesn't cost anything to declare object references, but when you instantiate objects, you gotta know you have started to eat the pie and it is going to get over soon
Hope that helps, and ofcourse, come back with questions if you don't understand something here. I would recommend you guys reading JVM Section 2.4 Types and Values and Section 2.5 Variables
Ajith
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith ...
Thanks for the great response. Clear as a bell, as always
------------------
Jane
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object references are in the stack memory and objects are created in the heap memory.
The stack pointer is moved down to allocate an object reference and moved up to remove it, which is not an expensive process, unlike an object creation in the heap memory.
[This message has been edited by Jon Aryan (edited October 09, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic