如果从分配内存的角度看,所有线程共享的 Java 堆中可以划分出多个线程私有的分配缓冲区 (Thread Local Allocation Buffer,TLAB) ,以提升对象分配时的效率
无法在拓展:java.lang.OutOfMemoryError: Java heap space
例子:模拟OOM,设置JVM参数-Xmx128m -Xms128m,这里最大最小都设置128M,我的Customer里面有个bytes变量,一个就有1m,所以每个CUstomer都多于1M,所以运行程序customerList长度到达一定次数就OOM了
java复制代码
@Data
public class Customer {
private int no;
private String username;
private BigDecimal money;
private byte[] a = new byte[1024 * 1024]; // 这里1024*1024byte等于1m
}
java复制代码
public class MyTestOOM {
public static void main(String[] args) {
List<Customer> customerList = new ArrayList<>();
while (true) {
Customer customer = new Customer();
customer.setNo(1);
customer.setUsername("testOOM");
customer.setMoney(new BigDecimal("1000"));
customerList.add(customer);
System.out.println(customerList.size());
}
}
}