Memory Leaks in Java
Memory Leak in Java

Memory Leaks in Java

After seeing the heading you might get an question like "Java will automatically takes care about memory management then how this leaks possible ?",

This is 100% right Java will take care of memory management with the help of in built garbage collector in short GC, these will takes care of allocating and freeing up memory, but.. but.. it doesn’t guarantee for foolproof solution for memory leaks. this GC is really pretty smart but not flawless.


What is Memory Leak ?

This is the situation where objects present in to heap that are no more in use but still in heap and GC is unable to clear those objects, this situation is Memory Leak.

Memory leak blocks the resources and degrades system performance over time, this will exhaust its resource finally we will face java.lang.OutOfMemoryError.

There are two objects present inside heap, Referenced and Un-Referenced, Referenced means whose reference is still active and un-referenced means who's reference is not active.

We have to know the main reason like even we have GC after that also how we will face this memory leak issues in java ?

Because, GC bhaii will clear Un-referenced objects periodically, but it never collect the objects which are still being referenced.


Now, what is symptoms of Memory Leak:

  1. Server performance degradation.
  2. OutOfMemoryError in the application.
  3. Strange application crashes.
  4. Application running on out of collection objects.


Types of Memory Leak and Prevent on it

  1. Static fields:a. In java, static fields have life that usually matches the entire running application lifecycle, unless ClassLoader becomes eligible for garbage collection.How to Prevent ?1. Less use of static2. Implementation that lazily loads the object instead of eagerly loading.
  2. Unclosed resource:a. Whenever we made new connection or open a stream, JVM allocates a memory for these resources. Ex. database connection, input streams, session objects.b. If we forgotten to close then these will out to reach for GC bhaii.How to Prevent ?1. Always use finally block to close resources.2. Where we are closes the resources shouldn't have any exception, not even inside finally block.
  3. Improper equals() and hashCode() Implementationsa. while defining new classes, common oversight is not writing proper overridden methods. HashMap and HashSet uses this methods in many operations.How to Prevent ?1. Always override equals() and hashCode() methods, not only override but proper implementation as well.
  4. Inner Classes That Reference Outer Classesa. Every non-static Inner Class has, by default, an implicit reference to its containing class. If we use this inner class’ object in our application, then even after our containing class’ object goes out of scope, it won’t be garbage collected.
  5. finalize() Methodsa. Use of finalizers is yet another source of potential memory leak issues. Whenever a class’?finalize() method is overridden, then objects of that class aren’t instantly garbage collected. Instead, the GC queues them for finalization, which occurs at a later point in time.


Strategies for Dealing With Memory Leaks

  1. Enable Profiling: using profilers, we can compare different approaches and find areas where we can optimally use our resources.
  2. Enabling Verbose Garbage Collection: we can track the detailed trace of the GC. To enable this, we need to add the following to our JVM configuration:

-verbose:gc        

3. Use of Referenced Objects


Conclusion:

Memory leaks are tricky to solve, and finding them requires intricate mastery and command over the Java language. While dealing with memory leaks, there’s no one-size-fits-all solution, as leaks can occur through a wide range of diverse events.





要查看或添加评论,请登录

Prajval Bhale的更多文章

社区洞察

其他会员也浏览了