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:
- Explicitly remove the reference to the short life cycle object
- Shorten the life cycle of the anchor
- Weaken the reference
You can use the methods below to tell whether your system has memory leak:
- 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.
- OutOfMemoryError - easy indicator
- 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






































(4.75 out of 5)
No Comment Received
Sorry the comment area are closed for non registered users