• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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: 41967
911
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.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic