Memory leak is possible in Java

I previously thought that if I pick Java for programming, I don’t need to worry about allocating and free up memory as in C++. Certainly, Java memory management does help us to minimize the chance of memory leak. However, it does not eliminate it. It is confusing right? The key is the definition of memory leak. If memory leak means an object allocated in the heap which has no direct reference, then Java does a good job to tackle this concern via object reachability (not reference counting). However, memory leak can be generalized to include the case that an object in memory that is not used but somehow being referenced and causing garbage collector not able to clean it. When will this happen in Java? A long lifecycle object holds a reference to a short lifecycle object. Below is the list of possible fixes:

  1. Explicitly remove the reference to the short life cycle object
  2. Shorten the life cycle of the anchor
  3. Weaken the reference

You can use the methods below to tell whether your system has memory leak:

  1. Examine heap memory status after several GCs. If you see the memory usage is trending up even after GCs, then you may have memory leak.
  2. OutOfMemoryError – easy indicator
  3. Performance decline – It may be the overhead of  page swapping. This will take place as memory is in demand.

Reference

Plugging memory leak with weak reference

Comments

comments

Subscribe

Subscribe my "7 Days Crack Course" to make money online together! Free for the first 100 registration.

No comments yet.

Leave a Reply