> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/java/java-xu-ni-ji/2.4-dui.md).

# 2.4堆

Heap堆

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

特点

* 它是线程共享的，堆中对象都需要考虑线程安全的问题
* 有垃圾回收机制

演示堆内存溢出

java.lang.outOfMemoryError: Java heap space

```java
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
