Home >Java >javaTutorial >What are the common memory leak scenarios in Java?
Common memory leak scenarios in Java include: holding references to external objects, static references, invalid listeners, thread-local variables, and circular references. Common memory leak scenarios in application servers include threads holding references to servlet objects, static holders holding references to persistent connections, and listeners not being removed from components.
Common memory leak scenarios in Java
Memory leaks are a serious flaw in software development that will develop over time. This can lead to application crashes or performance degradation. The following are the most common memory leak scenarios in Java:
1. Holding a reference to an external object
When an object holds a reference to an external object, The JVM cannot garbage collect external objects when they are not in use. For example:
class Outer { private Inner inner; public Outer() { inner = new Inner(); // 持有对 Inner 的引用 } } class Inner { // ... }
2. Static Reference
Static variables are stored in permanent memory in the JVM and they are never garbage collected. If a static variable holds a reference to an object, the object cannot be garbage collected. For example:
public class Example { private static List<Object> objects = new ArrayList<>(); public static void main(String[] args) { objects.add(new Object()); } }
3. Invalid Listener
A memory leak occurs when a listener is no longer used but is still attached to an event source. For example:
import javax.swing.*; public class ListenerLeak { private JButton button; public ListenerLeak() { button = new JButton(); button.addActionListener(e -> { // ... }); } // 忘记从按钮中移除监听器 }
4. Thread-local variables
Thread-local variables are stored in the thread-local storage (TLS) of each thread and remain active as long as the thread is active. They will not be garbage collected. If you use thread-local variables in a completed thread, you may cause a memory leak. For example:
public class ThreadLocalLeak { private static ThreadLocal<Object> threadLocal = new ThreadLocal<>(); public static void main(String[] args) { Thread thread = new Thread(() -> { threadLocal.set(new Object()); // 在线程中设置值 }); thread.start(); thread.interrupt(); // 中断线程 // 线程局部存储未得到清理 } }
5. Circular Reference
A circular reference occurs when two or more objects refer to each other. This causes the JVM to not recognize that they are no longer in use, resulting in a memory leak. For example:
public class CycleReference { private CycleReference other; public CycleReference() { other = new CycleReference(); other.other = this; // 循环引用 } }
Practical case
Memory leaks in application servers
The following are typical memory leaks in application servers Scenario:
You can reduce the risk of memory leaks in your Java applications by understanding common memory leak scenarios and adopting appropriate coding practices to ensure their stability and performance.
The above is the detailed content of What are the common memory leak scenarios in Java?. For more information, please follow other related articles on the PHP Chinese website!