JVM内存分析系列(二)内存溢出的类型分析

[b]启动参数[/b]
-server -verbose:gc -Xms10m -Xmx10m -Xss128k -Xloggc:C:/tmp/gc.log -XX:PermSize=5m -XX:MaxPermSize=5m -XX:MaxDirectMemorySize=10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:/tmp

[b]Java堆溢出[/b]
              List<Server> list = new ArrayList<Server>();
while (true) {
list.add(new Server());
}

java.lang.OutOfMemoryError: Java heap space
Dumping heap to C:/tmp\java_pid7032.hprof ...
Heap dump file created [17593217 bytes in 0.173 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.chinaso.phl.Server.main(Server.java:29)


[b]虚拟机栈和本地方法栈溢出[/b]

private int stackLength = 1;

public void stackLeak() {
stackLength++;
stackLeak();
}
public static void main(String[] args) throws Throwable {
Server s = new Server();
try {
s.stackLeak();
} catch (Throwable t) {
System.out.println("stack length:" + s.stackLength);
throw t;
}
}

stack length:1007
Exception in thread "main" java.lang.StackOverflowError
at com.chinaso.phl.Server.stackLeak(Server.java:13)


[b]运行时常量池溢出[/b]

List<String> list = new ArrayList<String>();
int i = 0;
while (true) {
list.add(String.valueOf(i++).intern());
}

java.lang.OutOfMemoryError: PermGen space
Dumping heap to C:/tmp\java_pid7448.hprof ...
Heap dump file created [2591479 bytes in 0.050 secs]
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
at java.lang.String.intern(Native Method)
at com.chinaso.phl.Server.main(Server.java:45)

[color=red]注意:如果写成下面的形式,就是堆溢出了。只有String.intern()方法,是把字符串内容放入常量池并返回。[/color]

List<String> list = new ArrayList<String>();
int i = 0;
while (true) {
list.add(new String(String.valueOf(i++)));
}

java.lang.OutOfMemoryError: Java heap space
Dumping heap to C:/tmp\java_pid8228.hprof ...
Heap dump file created [11828134 bytes in 0.151 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.chinaso.phl.Server.main(Server.java:46)


[b]方法区溢出[/b]
                while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOMObject.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj,
Method method,
Object[] args,
MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
enhancer.create();
}

java.lang.OutOfMemoryError: PermGen space
Dumping heap to C:/tmp\java_pid6624.hprof ...
Heap dump file created [1357998 bytes in 0.047 secs]
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
at net.sf.cglib.core.EmitUtils.member_switch_helper(EmitUtils.java:690)
at net.sf.cglib.core.EmitUtils.constructor_switch(EmitUtils.java:681)
at net.sf.cglib.proxy.Enhancer.emitNewInstanceMultiarg(Enhancer.java:849)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:508)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at com.chinaso.phl.Server.main(Server.java:66)


[b]本机直接内存溢出[/b]
        Field unsafeField = sun.misc.Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
sun.misc.Unsafe unsafe = (sun.misc.Unsafe) unsafeField.get(null);
while (true) {
unsafe.allocateMemory(1024 * 1024);
}

Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at com.chinaso.phl.Server.main(Server.java:70)


[b]附录[/b]
本文只是模拟各种内存溢出的情况,文章追求精简直接,本文不适合初学者,需要有一定的基础,对JVM内存模型有一定的了解。


作者简介
昵称:澳洲鸟
姓名:朴海林
QQ:85977328
MSN:6301655@163.com

转载请注明出处
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫头哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值