Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection .....

 
Muralidhar Gandepalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,All
I have this question troubling me for some time ..."Inspite of having Garbage Collector in a VM can we have memory leaks ?.If yes can any one give one or two examples of these leaks "
Thanx in advance
Murali
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Yes, we can have Memory Leaks as in C or C++.
Condider the following program:

It causes an OutOfMemoryError in seconds. Of cause, this is not a real world example. But it shows, that just those objects are garbage collected, that are not in use. Not in use means, that there is no reference to it, i.e. it is not possibe to access the object any longer.
The many byte-arrays in my example still have references to them -- one each (the vector "knows" them). So the garbage collector can't decide to throw them away.
Programming errors can cause a similar effect. Of cause, that would not be that abvious. But forgetting to release references (e.g. by setting them to null) definetly can cause that situation. And at least one of these errors caused a memory leak in one of my projects ...
I hope that helps.
Greetings from Hamburg,
Stefan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic