Reloading Java Classes 201: How do ClassLoader leaks happen? Translation
The original link : http://zeroturnaround.com/rebellabs/rjc201/
From ClassLoaders to Classes
从ClassLoader到Classes
If you have programmed in Java for some time you know that memory leaks do happen. Usually it’s the case of a collection somewhere with references to objects (e.g. listeners) that should have been cleared, but never were. Classloaders are a very special case of this, and unfortunately, with the current state of the Java platform, these leaks are both inevitable and costly: routinely causing OutOfMemoryError’s in production applications after just a few redeploys.
如果你已经使用java编程一段时间了, 你知道内存泄露在java中时有发生.常见的例子是集合中的对象(比如listeners)的引用该清理掉却没从没被清理.Classloader是这种情况的非常特殊的一个案例,而且非常不幸的是,Java平台的现状, 这些泄露不可避免而且造成的代价很高: 通常在几次redeploy以后再引用程序中就会造成OutOfMemoryError.
Let’s get started. Recalling RJC101: to reload a class we threw away the old classloader and created a new one, copying the object graph as best we could:
让我们开始吧. 回顾 RJC101: reload一个class我们丢弃了旧的classloader并且创建了一个新的, 然后尽力复制原来的对象图:

Every object had a reference to its class, which in turn had a reference to its classloader. However we didn’t mention that every classloader in turn has a reference to each of the classes it has loaded, each of which holds static fields defined in the class:
每一个object都有一个指向它的class的引用, 同时它的class也有一个指向它的classloader的引用.然而我们并没有提到每个classloader反过来也对每个载入的class持有一个引用, 每个class都有一个static的field

This means that
这意味着
- If a classloader is leaked it will hold on to all its classes and all their static fields. Static fields commonly hold caches, singleton objects, and various configuration and application states. Even if your application doesn’t have any large static caches, it doesn’t mean that the framework you use doesn’t hold them for you (e.g. Log4J is a common culprit as it’s often put in the server classpath). This explains why leaking a classloader can be so expensive.
- To leak a classloader it’s enough to leave a reference to any object, created from a class, loaded by that classloader. Even if that object seems completely harmless (e.g. doesn’t have a single field), it will still hold on to its classloader and all the application state. A single place in the application that survives the redeploy and doesn’t do a proper cleanup is enough to sprout the leak. In a typical application there will be several such places, some of them almost impossible to fix due to the way third-party libraries are built. Leaking a classloader is therefore, quite common.
- 假如一个classloader泄露了, 他将会持有所有他的class和他们的static field.Static field通常是cache, 单例 object, 和变化的配置及程序状态变量.即使你的程序没有任何打的静态cache, 也不代表你用的框架没有(比如Log4J就是一个常见的罪魁祸首, 因为它经常被load进server的classpath中). 这解释了为什么泄露一个classloader会造成很严重的问题
- 泄露一个classloader会让任何对象被这个classloader加载的class创建的object的引用孤立. 即使这个object看起来是完全无害的(这个object没有任何一个单独的filed),他依然会被他的classloader和所有application state持有.一块在redepoly中存活下来的application中的独立的空间如果没有做一个合适的清理,就足以造成这种泄露.一个典型的application会有好几个这样的空间, 有一些由于第三方库的构建方法,甚至是不可能修复的. 所以说classloader的泄露太常见了.
To examine this from a different perspective let’s return to the code example from our previous article. Breeze through it to quickly catch up.
为了用另一种视角来解释这种现象,我们回到上一个章节提到的代码.快速的过一下:
Introducing the Leak
泄露介绍
We will use the exact same Main class as before to show what a simple leak could look like:
我们会使用一个和前面完全一样的Main class来演示一个简单的泄露
1 |
public class Main {
|
The ExampleFactory class is also exactly the same, but here’s where things get leaky. Let’s introduce a new class called Leak and a corresponding interface ILeak:
ExampleFactory class也是完全一样的, 但这里就是泄露发生的地方. 我们介绍一下Leak和他对应的接口ILeak
1 |
interface ILeak {
|
As you can see it’s not a terribly complicated class: it just forms a chain of objects, with each doing nothing more than holding a reference to the previous one. We will modify the Example class to include a reference to the Leak object and throw in a large array to take up memory (it represents a large cache). Let’s omit some methods shown in the previous article for brevity:
如你所见这是一个简单的class: 他只是构造了一个对象链, 除了持有一个对象引用啥也没干.我们修改Example类将Leak包含进来, 然后创建一个大数组中来占据内存空间(他代表了一个大cache). 为了简单起见我们忽略了上一篇文章提到的一些方法:
1 |
public class Example implements IExample {
|
The important things to note about Example class are:
Example类中最重要的是
Exampleholds a reference toLeak, butLeakhas no references toExample.- When
Exampleis copied (methodcopy()is called) a newLeakobject is created holding a reference to the previous one.
- Example持有一个Leak的引用,但是Leak中没有对Example的引用
- 当Example被copy(copy()方法被调用),一个新的Leak对象就会被创建并持有前一个Leak的引用
If you try to run this code an OutOfMemoryError will be thrown after just a few iterations:
尝试运行这段代码过不久就会造成OutOfMemoryError
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space |
With the right tools, we can look deeper and see how this happens.
使用正确的工具, 我们可以更深入的看到OOM是怎么发生的
Post Mortem
Since Java 5.0, we’ve been able to use the jmap command line tool included in the JDK distribution to dump the heap of a running application (or for that matter even extract the Java heap from a core dump). However, since our application is crashing we will need a feature that was introduced in Java 6.0: dumping the heap on OutOfMemoryError. To do that we only need to add -XX:+HeapDumpOnOutOfMemoryError to the JVM command line:
从Java 5.0开始,我们可以使用JDK中的jmap命令行工具来导出运行程序的heap(对于这个问题甚至可以将整个Java的heap从core中导出).然而, 因为我们的程序crash了, 我们需要一个Java6.0中的特性: 在OutOfMemoryError时导出heap.为了做到这点我们仅仅需要加入命令 -XX:+heapDumpOnOutOfMemoryError 到JVM命令行中:
java.lang.OutOfMemoryError: Java heap space |
After we have the heap dump we can analyze it. There are a number of tools (including jhat, a small web-based analyzer included with the JDK), but here we will use the more sophisticatedEclipse Memory Analyzer (EMA).
导出heap后我们可以开始分析了. 这里有好几种工具可以使用(包括 jhat, JDK里一个小巧的基于web的分析器), 但是这里我们会使用精密的Eclipse Memory Analyzer(EMA)
After loading the heap dump into the EMA we can look at the Dominator Tree analysis. It is a very useful analysis that will usually reliably identify the biggest memory consumers in the heap and what objects hold a reference to them. In our case it seems quite obvious that the Leak class is the one that consumes most of the heap:
通过载入heap dump到EMA, 我们可以看看Dominator Tree的分析结果. 这是非常有用的分析, 他可靠的辨认出了heap里最大的内存消耗者和什么对象持有这些消耗者的引用.在我们的例子里很明显Leak就是最大的消耗者:

Now let’s run a search for all of the Leak objects and see what are they holding to. To do that we run a search List objects -> with outgoing references for “example.Leak”:
现在让我们搜索一下所有的Leak对象并看看他们都持有什么.按下面步骤运行 对example.Leak执行 List Objects -> with outgoing references

The results include several Leak objects. Expanding the outgoing references we can see that each of them holds on to a separate instance of Example through a bunch of intermediate objects:
这些结果包括一些Leak对象. 展开这些引用,我们可以看到,他们每个都通过一群中间对象持有一个Example对象实例

You may notice that one of the intermediate objects is ExampleFactory$1, which refers to the anonymous subclass of URLClassLoader we created in the ExampleFactory class. In fact what is happening is exactly the situation we described in the beginning of the article:
你可能注意到了其中一个中间对象是ExampleFactory$1, 他指向的是我们在ExampleFactory类里创建的URLClassLoader的一个匿名子类. 事实上发生的事情正是我们在文章开头讨论的情况
- Each
Leakobject is leaking. They are holding on to their classloaders - 每个Leak都在泄露.他们持有他们的classloader
- The classloaders are holding onto the
Exampleclass they have loaded: - classloader持有所有他们已经load的class

Conclusions
结论
Though this example is slightly contrived, the main idea to take away is that it’s easy to leak a single object in Java. Each leak has the potential to leak the whole classloader if the application is redeployed or otherwise a new classloader is created. Since preventing such leaks is very challenging, it’s a better idea to use Eclipse Memory Analyzer and your understanding of classloaders to hunt them down after you get an OutOfMemoryError on redeploy.
虽然这个例子有点人为而之, 但他的中心思想是在Java中泄露一个object. 假如程序被redeployed或者创建新的classloader,那么每次泄露都有潜在的泄露整个classloader的危险. 因为防止这样的泄露是很难的, 所以最好的主义是在你redeploy以后获得OutOfMemoryError是使用EMA和你对classloader的理解来解决这个问题.
This article addressed the following questions:
这篇文章提出了以下问题
- How does reloading a class cause the classloader to leak?
- 如何重加载一个class会导致classloader泄露
- What are some consequences of leaking classloaders?
- 泄露classloader的后果是什么
- What tools can be used to troubleshoot these memory leaks?
- 什么工具可以用来排除这些内存泄露故障
Resources
- How to fix the dreaded “java.lang.OutOfMemoryError: PermGen space” exception (classloader leaks)
- Download full source code
Reloading Java Classes 201: How do ClassLoader leaks happen? Translation的更多相关文章
- Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation
The Original link : http://zeroturnaround.com/rebellabs/rjc301/ Copyright reserved by Rebel Inc In t ...
- Reloading Java Classes 101: Objects, Classes and ClassLoaders Translation
The original link: http://zeroturnaround.com/rebellabs/reloading-objects-classes-classloaders/ A Bir ...
- Java classes and class loading
JAVA类加载器概念与线程类加载器 http://www.cnblogs.com/pfxiong/p/4118445.html http://stackoverflow.com/questions/2 ...
- The differences between Java EE components and "standard" Java classes
https://docs.oracle.com/javaee/7/tutorial/overview003.htm ava EE components are written in the Java ...
- When a java class is load by classloader, where the constant poll be put?
Q:When a java class is load by classloader, where the constant poll be put? A:the "Non-Heap Mem ...
- java中class.forName和classLoader加载类的区分
java中class.forName和classLoader都可用来对类进行加载.前者除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块.而classLoade ...
- Thread.currentThread().setContextClassLoader为什么不生效与java.lang.NoClassDefFoundError之Java类加载的Parent first Classloader
众所周知,Java的类加载机制采用了双亲委派模型,导致在进行类加载的时候会有多个加载器,这种复杂的机制,有时候会导致‘Exception in thread main java.lang.NoClas ...
- 一篇文章带你吃透,Java界最神秘技术ClassLoader
ClassLoader 是 Java 届最为神秘的技术之一,无数人被它伤透了脑筋,摸不清门道究竟在哪里.网上的文章也是一篇又一篇,经过本人的亲自鉴定,绝大部分内容都是在误导别人.本文我带读者彻底吃透 ...
- Java:类加载器(ClassLoader)
听上去很高端,其实一般自定义类加载器不需要用户去实现解析的过程,只要负责实现获取类对应的.class字节流部分就ok了,摘录深入理解Java虚拟机的一段话 虚拟机设计团队把类加载阶段中的“通过一个类的 ...
随机推荐
- Windows下PHP多线程扩展pthreads的安装
pthreads扩展安装步骤 1.查看phpinfo() 获取PHP版本号及位数(x86表示32位,x64表示64位).编译器版本.PHP配置文件加载所在位置等.如下图所示: 2.pthreads扩展 ...
- PowerShell 中使用 mvn 编译报错 Unknown lifecycle phase ".test.skip=true". 解决办法
nknown lifecycle phase “–Dmaven.test.skip=true”. You must specify a valid lifecycle phase or a goal ...
- BZOJ.4571.[SCOI2016]美味(主席树 贪心)
题目链接 要求 \(b\ xor\ (a_j+x)\) 最大,应让 \(a_j+x\) 的最高位尽可能与b相反.带个减法Trie树好像很难做?反正我不会. 从最高位开始,如果这位b是0/1,判断是否存 ...
- 关于操作Access数据库jdk选择问题
关于操作Access数据库,使用jdk64位无法通过ODBC无法获取数据,只能通过jdk32位进行开发.
- OpenNI2 + NiTE2开发教程
发现了一个非常不错的关于自然交互OpeNI2+NiTE2的资源,非常感谢Heresy,这里分享链接: OpenNI 2.x 教学文章(转载自:Heresy博客,地址:https://kheresy.w ...
- C#中执行Dos命令
//dosCommand Dos命令语句 public string Execute(string dosCommand) { ); } /// <summary> /// 执行DOS命令 ...
- HDU 4739 Zhuge Liang's Mines (2013杭州网络赛1002题)
Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- LPCScrypt, DFUSec : USB FLASH download, programming, and security tool, LPC-Link 2 Configuration tool, Firmware Programming
What does this tool do? The LPC18xx/43xx DFUSec utility is a Windows PC tool that provides support f ...
- dbMigration .NET 数据同步迁移工具
官网:http://fishcodelib.com/DBMigration.htm
- dos下 和 批处理中的 for 语句的基本用法
for 语句的基本用法 : 最复杂的for 语句,也有其基本形态,它的模样是这样的: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for % ...