java direct memory_第一讲  JVM内存四大类型:Heap,Stack,Contant,DirectMemory等

第一讲JVM内存四大类型:Heap,Stack,Contant,DirectMemory等

Stack属于栈的区域,属于每条线程私有的。

a4c26d1e5885305701be709a3d33442f.png

方法区和本地方法栈有很大的不同,方法区是用Java级别角度做的代码,本地方法栈指向的是C/C++。

Java开发,对象就在堆中,一般而言,堆中只有对象。

堆溢出测试:程序运行设置:-verbose:gc -Xms10M -Xmx10M -Xss128k

-XX:+PrintGCDetails

package

com.dt.spark.jvm.basics;

import

java.util.ArrayList;

import

java.util.List;

class

Person{ }

public

class HelloHeapOutOfMemory {

public static void main(String[] args) {

System.out.println("HelloHeapOutOfMemory");

Listpersons = new ArrayList();

int counter = 0;

while(true){

persons.add(new

Person());

System.out.println("Instance:

" + (++counter));

}

}

}

报错:Exception in thread "main"

java.lang.OutOfMemoryError: Java heap space

栈溢出测试:

package com.dt.spark.jvm.basics;

public classHelloStackOverFlow {

private int counter;

publicvoidcount()

{

counter++;

count();

}

public static void main(String[] args)

{

System.out.println("HelloStackOverFlow");

HelloStackOverFlow helloStackOverFlow=

newHelloStackOverFlow();

try {

helloStackOverFlow.count();

} catch(Exception e)

{

e.printStackTrace();

throw e;

}

}

}

报错:Exception in thread "main"

java.lang.StackOverflowError

常量区溢出报错测试:

package

com.dt.spark.jvm.basics;

import

java.util.ArrayList;

import

java.util.List;

public

class HelloConstantOutOfMemory{

public static void main(String[] args) {

try {

List stringList = new ArrayList();

int item = 0;

while(true){

stringList.add(String.valueOf(item++).intern());

}

} catch (Exception e) {

e.printStackTrace();

throw e;

}

}

}

报错:Exception in thread "main" [Full GC (Ergonomics) java.lang.OutOfMemoryError: GC overhead limit

exceeded

DirectMemory溢出报错测试:

package com.dt.spark.jvm.basics;

import java.nio.ByteBuffer;

public classHelloDirectMemoryOutOfmemory {

private static final int ONE_GB =

1024*1024*1024;

private staticintcount=

1;

public static void main(String[] args)

{

try {

while (true)

{

ByteBuffer buffer =

ByteBuffer.allocateDirect(ONE_GB);

count++;

}

} catch (Exception e)

{

System.out.println("Exception:instance created "+count);

e.printStackTrace();

} catch (Error e)

{

System.out.println("Error:instance created "+count);

e.printStackTrace();

}

}

}

报错:java.lang.OutOfMemoryError: Direct buffer

memory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值