This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
Will Myers
Ranch Hand
Posts: 417
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
Posts: 22374
42
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!