Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Memory Leak

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is memory leak in Java? How to avoid it?
 
author & internet detective
Posts: 40246
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic