2.4堆

Heap堆

  • 通过new关键字,创建对象都会使用堆内存

特点

  • 它是线程共享的,堆中对象都需要考虑线程安全的问题

  • 有垃圾回收机制

演示堆内存溢出

java.lang.outOfMemoryError: Java heap space

public class Demo_5{

    public static void main(String[] args){
        int i=0;
        try{
            List<String> list =new ArrayList();
            string a ="hello";
            while(true){
                list.add(a);
                a=a+a;
                i++;
            }
        }catch(Throwble e){
            e.printStackTrace();
            System.out.println(i);
        }
    }
}

注意调整堆内存,可使用-Xmx参数,如-Xmx8m

Last updated

Was this helpful?