Skip to content

Commit 2741d4a

Browse files
committed
内容格式修改
在每一步后面添加了空白行,改进显示效果
1 parent 2dc09d9 commit 2741d4a

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

contents/creating-a-memory-leak-with-java.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55

66
## 解答
77
在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。
8+
89
1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。
10+
911
2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B
12+
1013
3. 在类B申请一块很大的连续内存(例如:new byte[1000000]),
1114
并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。
1215
虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。
16+
1317
4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。
18+
1419
5. 重复上述步骤
1520

1621
上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用,

0 commit comments

Comments
 (0)