• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashmap-- garbage collection  RSS feed

 
Sunetra Sen
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Suppose I have a hashmap which holds 5 keys of type Integer and corresponding 5 Strings.
So in total I have 5+5+1=11 objects on the heap.
Now I clone one of the String objects in the HashMap.After that I set the HashMap to null.
At this point of time how many objects are elligilble for garbage collection?(assume that the other String/Integer objects are not referenced by any other code)

Please let me know?
Thanks in advance,
Sunetra.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cloning of an object will not modify it.
Neither will the cloned object will have something special, indicating it was created by cloning.
So the cloning is really irrelevant.

I don't know whether setting the HashMap to null will make it ready for garbage-collection, before the name is leaving it's scope.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Discussion of GC involving String objects is always complicated by the possibility that the references are to String objects in the String pool. Assuming all those String objects were created with new then I would say you have 11 objects that have been made GC-able by setting the Hashtable reference to null.
Life is further complicated by the fact that the char[] that holds the data of the String may be shared by multiple Strings - take a look at the source code for java.lang.String - it is full of surprises.
Bill
 
Sunetra Sen
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for sharing the info!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!