Table of Contents
Preface
Case and Analysis
Problem Background
Solution to the problem
WebappClassLoader
Class life cycle and class loader
Reference diagram
总结
课后题
提示
Home Java javaTutorial Sharing code examples of ThreadLocal memory leaks in Java (picture)

Sharing code examples of ThreadLocal memory leaks in Java (picture)

Mar 23, 2017 am 10:47 AM

Preface

I previously wrote an in-depth analysis of the memory leak problem of ThreadLocal. It is a theoretical analysis of the memory leak problem of <span class="wp_keywordlink">ThreadLocal</span>. Let’s take a look at this article. Analyze actual memory leak cases. The process of analyzing the problem is more important than the result. Only by combining theory with practice can the cause of the memory leak be thoroughly analyzed.

Case and Analysis

Problem Background

In Tomcat, the following codes are all in webapp, which will cause WebappClassLoader to leak and cannot be recycled.

public class MyCounter {
        private int count = 0;

        public void increment() {
                count++;
        }

        public int getCount() {
                return count;
        }
}

public class MyThreadLocal extends ThreadLocal<MyCounter> {
}

public class LeakingServlet extends HttpServlet {
        private static MyThreadLocal myThreadLocal = new MyThreadLocal();

        protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {

                MyCounter counter = myThreadLocal.get();
                if (counter == null) {
                        counter = new MyCounter();
                        myThreadLocal.set(counter);
                }

                response.getWriter().println(
               "The current thread served this servlet " + counter.getCount()
                  + " times");
                counter.increment();
        }
}
Copy after login

In the above code, as long as LeakingServlet is called once and the thread executing it does not stop, it will cause WebappClassLoader to leak. Every time you reload the application, there will be one more instance of WebappClassLoader, which will eventually lead to PermGen OutOfMemoryException.

Solution to the problem

Now let’s think about it: Why does the above ThreadLocal subclass cause a memory leak?

WebappClassLoader

First of all, we have to figure out what WebappClassLoader is?

For web applications running in Java EE containers, the class loader is implemented differently from general Java applications. Different web containers are implemented differently. In the case of Apache Tomcat, each web application has a corresponding class loader instance. This class loader also uses the proxy mode. The difference is that it first tries to load a certain class. If it cannot find it, it then proxy to the parent class loader. This is the opposite of the normal class loader order. This is a recommended practice in the Java Servlet specification, and its purpose is to give priority to the Web application's own classes over those provided by the Web container. An exception to this proxy pattern is that Java core library classes are not included in the search. This is also to ensure the type safety of the Java core library.

That is to say WebappClassLoader is a custom class loader for Tomcat to load webapps. The class loader of each webapp is different. This is to isolate the loading of different applications. the type.

So what does WebappClassLoader’s features have to do with memory leaks? It's not visible yet, but it has a very important feature worthy of our attention: each webapp will have its own WebappClassLoader, which is different from the Java core class loader.

We know that the leakage of WebappClassLoader must be because it is strongly referenced by other objects, then we can try to draw their reference relationship diagram. etc! What exactly is the role of a class loader? Why is it strongly quoted?

Class life cycle and class loader

To solve the above problem, we have to study the relationship between the class life cycle and the class loader.

The main thing related to our case is the uninstallation of classes:

After the class is used, if the following conditions are met, the class will be uninstalled:

  1. All instances of this class have been recycled, that is, there are no instances of this class in the Java heap.

  2. The ClassLoader that loaded this class has been recycled.

  3. The java.<a href="http://www.php.cn/java/java-ymp-Lang.html" target="_blank">lang</a>.Class object corresponding to this class is not referenced anywhere, and this class is not accessed through reflection anywhere. method.

If all the above three conditions are met, the JVM will uninstall the class during garbage collection in the method area. The uninstallation process of the class is actually to clear the class information in the method area. Java The entire life cycle of the class is over.

Classes loaded by the class loader that comes with the Java virtual machine will never be unloaded during the life cycle of the virtual machine. The class loaders that come with the Java virtual machine include root class loaders, extension class loaders and system class loaders. The Java virtual machine itself will always refer to these class loaders, and these class loaders will always refer to the Class objects of the classes they load, so these Class objects are always reachable.

Classes loaded by user-defined class loaders can be unloaded.

Pay attention to the above sentence, WebappClassLoaderIf it leaks, it means that the classes it loads cannot be unloaded, which explains why the above code will cause PermGen OutOfMemoryException.

Key points look at the picture below

We can find that the class loader object is bidirectionally related to the Class object it loads. This means that the Class object may be a strong reference to WebappClassLoader, causing it to leak.

Reference diagram

After understanding the relationship between the class loader and the life cycle of the class, we can start to draw the reference diagram. (The references to LeakingServlet.class and myThreadLocal in the picture are not rigorous, mainly to express that myThreadLocal is the class variable)

Sharing code examples of ThreadLocal memory leaks in Java (picture)

下面,我们根据上面的图来分析WebappClassLoader泄漏的原因。

  1. LeakingServlet持有staticMyThreadLocal,导致myThreadLocal的生命周期跟LeakingServlet类的生命周期一样长。意味着myThreadLocal不会被回收,弱引用形同虚设,所以当前线程无法通过ThreadLocal<a href="http://www.php.cn/code/8210.html" target="_blank">Map</a>的防护措施清除counter的强引用。

  2. 强引用链:thread -> threadLocalMap -> counter -> MyCounter.class -> WebappClassLocader,导致WebappClassLoader泄漏。

总结

内存泄漏是很难发现的问题,往往由于多方面原因造成。ThreadLocal由于它与线程绑定的生命周期成为了内存泄漏的常客,稍有不慎就酿成大祸。

本文只是对一个特定案例的分析,若能以此举一反三,那便是极好的。最后我留另一个类似的案例供读者分析。

课后题

假设我们有一个定义在 Tomcat Common Classpath 下的类(例如说在 tomcat/lib 目录下)

public class ThreadScopedHolder {
        private final static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();

        public static void saveInHolder(Object o) {
                threadLocal.set(o);
        }

        public static Object getFromHolder() {
                return threadLocal.get();
        }
}
Copy after login

两个在 webapp 的类:

public class MyCounter {
        private int count = 0;

        public void increment() {
                count++;
        }

        public int getCount() {
                return count;
        }
}
public class LeakingServlet extends HttpServlet {

        protected void doGet(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {

                MyCounter counter = (MyCounter)ThreadScopedHolder.getFromHolder();
                if (counter == null) {
                        counter = new MyCounter();
                        ThreadScopedHolder.saveInHolder(counter);
                }

                response.getWriter().println(
                           "The current thread served this servlet " + counter.getCount()
                                                + " times");
                counter.increment();
        }
}
Copy after login

提示

Sharing code examples of ThreadLocal memory leaks in Java (picture)

The above is the detailed content of Sharing code examples of ThreadLocal memory leaks in Java (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles