• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Will Myers
Ranch Hand
Posts: 375
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have come across some code in our application:



I have asked the developer why it's there and why he doesn't just use collection = null, his answer is that calling = null will leave the objects in the collection stuck and they won't be garbage collected thus causing a memory leak. Can someone confirm if this is true? I always thought that if objecta references objectb but objecta is not referenced then both would be garbage collected.
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's false. You are correct.

However... it's also worth noting that setting the collection parameter to null is pointless. Since parameters are passed by value, this doesn't set the original variable which was passed to the method to null anyway. Perhaps that is the real reason why the collection doesn't actually get garbage collected.
 
susha bhogs
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems the developer is right. Because when we passed the objects - at the JVM its by reference only. Also, collection passes it by reference [similar to shallow cloning]

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic