Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory Leaks - Where and How

 
veerabahu Subramanian
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code is a snippet from http://www.performancewiki.com/memory-leak-analysis.html

In that it is specified that above code leaks 1000000000 object. But i don't understand how and where. From my understanding the code tries to populate 1000000000 different strings in hashset. This HashSet is used for manipulation on different methods, so declared as a global object. In this case, may I know where is the leak and how to come out of it.

TIA
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends what you mean by "leak".

In languages like C++ :roll: , you have to allocate and deallocate memory explicitly. If you forget to deallocate some memory, and you no longer have any pointers to that memory, then the memory is genuinely leaked; nothing but an app restart will reclaim it.

In Java, deallocation occurs automatically by garbage collection, some time after the last reference to the memory has gone away. So you can never have a C++-style leak.

However, if you create a reference and keep that reference long beyond the time for which it's required, you have a sort of leak. The most common way of doing this is putting a reference in a global collection, and leaving it there long after it is no longer needed.

The code example you show is trying to demonstrate this. As you correctly point out, it's only a leak if you accept that the references in the collection are unnecessary.
[ May 02, 2008: Message edited by: Peter Chase ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!