1

This is regarding memory leak in Java. I want to know, What is exactly memory leak in java? What are the different factors or code mistakes due to which memory leak happened? How can I identify memory leak in given code?

These are mostly question get used to ask in interview and I want to know about these in detail. Can someone please explain and help me.

Learner
  • 261
  • 1
  • 4
  • 16
  • 1
    https://www.baeldung.com/java-memory-leaks#:~:text=A%20Memory%20Leak%20is%20a,degrades%20system%20performance%20over%20time. Might help – seenukarthi Jul 05 '21 at 08:58
  • This question is too broad for stackoverflow. Please read https://stackoverflow.com/help/asking – seenukarthi Jul 05 '21 at 08:59
  • @KarthikeyanVaithilingam Thanks for baeldung.com/…. , it's helped me a lot to understand memory leak problem. – Learner Jul 07 '21 at 01:38

2 Answers2

2

Memory leaks in Java usually do NOT happen.

Not in the classical sense: in other programming languages, you often have to free memory manually. This does not happen in Java, because usually the Garbage Collector removes unused data for you.

But, in Java, you can still run out of memory. The closest thing in Java to a "memory leak" is that you keep References to Objects you do no longer need, and the objects getting more and more.

So maybe you have a list with a lot of objects, and forget to remove them from the list even though you don't need them anymore. That way the Garbage Collector cannot remove the unneeded objetcs, because they're still needed.

Also check google for WeakReference

JayC667
  • 2,418
  • 2
  • 17
  • 31
2

However, this question is far too crude to get a completely correct answer here. I have only shown you common behavior patterns.

What is a memory leak?
A Memory Leak is a situation when there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory and, thus they are unnecessarily maintained. A memory leak is bad because it blocks memory resources and degrades system performance over time.

What causes memory leaks in Java?
The first scenario that might cause a Java memory leak is referencing a heavy object with a static field. We created our ArrayList as a static field – which will never be collected by the JVM Garbage Collector during the lifetime of the JVM process, even after the calculations it was used for are done.

Symptoms of a Memory leak:
Works fast at first, but slows over time.

  • Works fine with small data sets, severe performance issues with large data sets
  • Ever increasing Old-Generation memory usage in your JVM
  • Out-of-Memory Heap errors in your JVM
  • Spontaneous crashes.

Common memory leaks:

File/Text buffers not closed.
Hash maps keeping references alive if equals() and hashcode() are not implemented, e.g.

import java.util.Map;
public class MemLeak {
    public final String key;
    public MemLeak(String key) {
        this.key = key;
    }

    public static void main(String args[]) {
        try {
            Map map = System.getProperties();
            for(;;) { map.put(new MemLeak("key"), "value"); }
        } 
        catch(Exception e) { e.printStackTrace(); }
    }
}

How to fixing them?
There are two approaches. The first is a 'quick fix' attempt. If that fails then you'll have to go down the long road.

  1. Quick fix: Eclipse Memory Leak Warnings (catches some leaks)
  2. Manually disable & enable parts of your code and observe memory usage of your JVM using a JVM tool like VisualVM (or Jconsole, or Thermostat).
41 72 6c
  • 1,600
  • 5
  • 19
  • 30