线程池实验
程序员文章站
2022-03-08 22:49:28
...
内存设置(java8)
Java8默认的初始堆内存、最大堆内存根据系统而不同而不同,如下表所示:
参数名称 | 含义 | 默认值 | 说明 |
---|---|---|---|
-Xms | 初始堆内存 | 物理内存的1/64 | 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制. 等价于-XX:InitialHeapSize |
-Xmx | 最大堆内存 | 物理内存的1/4 | 默认(MaxHeapFreeRatio参数可以调整)空余堆内存大于70%时,JVM会减少堆直到 -Xms的最小限制 等价于-XX:MaxHeapSize |
oracle官方手册里提到,默认的大小根据系统的不同而不同,比如-client模式还是-server模式。
查看默认参数
java -XX:+PrintFlagsFinal -version
会打印所有参数。其中InitialHeapSize就是-Xms,MaxHeapSize就是-Xmx。
由于上述的参数打印的结果较多,可以在windows上使用检索命令:
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
linux上检索命令:
java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
或者换种方法查看常设置的参数:比如在一台4G的linux机器上查看:
java -XX:+PrintCommandLineFlags -version
输出结果:
-XX:InitialHeapSize=64487744 (约61MB,约内存的1/64)
-XX:MaxHeapSize=1031803904 (约984MB,约内存的1/4)
-XX:+PrintCommandLineFlags
-XX:+UseCompressedClassPointers
-XX:+UseCompressedOops
-XX:+UseParallelGC
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
在一台8G内存的linux机器上查看:
输出结果:
-XX:InitialHeapSize=130802560 (约124MB,约内存的1/64)
-XX:MaxHeapSize=2092840960 (约1995MB,约内存的1/4)
-XX:+PrintCommandLineFlags
-XX:+UseCompressedClassPointers
-XX:+UseCompressedOops
-XX:+UseParallelGC
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
自定义堆内存大小
-Xmx20m -Xms5m
FixedThreadPool
//-Xmx20m -Xms5m
public class ThreadPoolExceptionTest1 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(1);
while(true){
es.submit(()->{
byte[] b = new byte[1 * 1024 * 1024];
try {
TimeUnit.SECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
}
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.concurrent.Executors.callable(Executors.java:407)
at java.util.concurrent.FutureTask.<init>(FutureTask.java:152)
at java.util.concurrent.AbstractExecutorService.newTaskFor(AbstractExecutorService.java:87)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:111)
//-Xmx20m -Xms5m
public class ThreadPoolExceptionTest1 {
public static void main(String[] args) {
//ExecutorService es = Executors.newFixedThreadPool(1);
ExecutorService es = Executors.newSingleThreadExecutor();
//ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < 6665535; i++) {
es.execute(()->{
byte[] b = new byte[1 * 1024 * 1024];
System.out.println("进入sleep");
try {
Thread.sleep(50_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
}
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
//-Xmx20m -Xms5m
public class ThreadPoolExceptionTest1 {
public static void main(String[] args) {
//ExecutorService es = Executors.newFixedThreadPool(1);
//ExecutorService es = Executors.newSingleThreadExecutor();
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < 6665535; i++) {
es.execute(()->{
byte[] b = new byte[1 * 1024 * 1024];
System.out.println("进入sleep");
try {
Thread.sleep(50_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
}
Exception in thread "pool-1-thread-578" java.lang.OutOfMemoryError: Java heap space
下一篇: EL表达式隐式对象