Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Memory Leak

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is memory leak in Java? How to avoid it?
 
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, you don't have true memory leaks like in C++. Java's garbage collection recovers memory so it doesn't have to be explicitly freed.
However if you keep references to objects you don't need, you will waste memory. For example, if I have and keep adding items to the list, it will grow througout the life of the program. The JVM will not know to release the objects until I explictly null them out.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the Garbage Collector ... Must I explicitly set an Object / variable to null in order for it to collect, or does it recognize scope?
onlyOOD
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It recognizes scope, as soon as there is no reference to it left it should collect it. I seem to remember reading about an odd quirk with a collection (I think it was a collection) where objects in the collection don't have their resources reclaimed even when that part of the collection has been removed, but I'm not entirely sure, I'll try and find where I read that.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One important thing to remember about garbage collection (gc) is that you as a programmer can never force it to run. There's no way to ensure that at a particular point in your code all of the memory that you can no longer use will be cleaned up. Even if you have the following line of code in your program, gc isn't compelled to run:

Gc may run in this situtation, but the JVM may decide not to for some reason.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic