• 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: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic