JDK7与8内存参数解析与修改
JDK内存
jdk内存实际是jvm内存,jvm有一个运行时数据区,其实就是对这一部分的大小分配。
运行时数据区通常包括这几个部分:程序计数器(Program Counter Register)、Java栈(VM Stack)、本地方法栈(Native Method Stack)、方法区(Method Area)、堆(Heap)
- Xss:每个线程的stack大小(栈)
- Xmx:JAVA HEAP的最大值、默认为物理内存的1/4
- Xms:JAVA HEAP的初始值,server端最好Xms与Xmx一样
- Xmn:JAVA HEAP young区的大小(相当于对-XX:newSize、-XX:MaxnewSize同时设置)
- XX:PermSize:设定内存的永久保存区域(JDK7)
- XX:MaxPermSize:设定最大内存的永久保存区域(JDK7)
JDK1.7调节参数与查看参数:
Administrator@MicroWin10-1535 MINGW64 ~/Desktop/test $ javac Test.java Administrator@MicroWin10-1535 MINGW64 ~/Desktop/test $ java -Xms1024m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=512m Test
C:\Users\Administrator>jps 19284 Jps 6628 NetworkServerControl 18072 22340 21860 Test C:\Users\Administrator>jmap -heap 21860 Attaching to process ID 21860, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.80-b11 using thread-local object allocation. Parallel GC with 4 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 1073741824 (1024.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 536870912 (512.0MB) MaxPermSize = 536870912 (512.0MB) G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 268959744 (256.5MB) used = 10758632 (10.260231018066406MB) free = 258201112 (246.2397689819336MB) 4.000090065522965% used From Space: capacity = 44564480 (42.5MB) used = 0 (0.0MB) free = 44564480 (42.5MB) 0.0% used To Space: capacity = 44564480 (42.5MB) used = 0 (0.0MB) free = 44564480 (42.5MB) 0.0% used PS Old Generation capacity = 716177408 (683.0MB) used = 0 (0.0MB) free = 716177408 (683.0MB) 0.0% used PS Perm Generation capacity = 536870912 (512.0MB) used = 2614432 (2.493316650390625MB) free = 534256480 (509.5066833496094MB) 0.48697590827941895% used 1523 interned Strings occupying 140352 bytes.
当然了:还有另外的配置,只是不常用
-XX:NewSize:新生代初始化内存的大小(注意:该值需要小于-Xms的值)。
-XX:MaxNewSize:新生代可被分配的内存的最大上限(注意:该值需要小于-Xmx的值)。
-Xmn:对-XX:newSize、-XX:MaxnewSize两个参数同时进行配置(注意:JDK1.4之后才有该参数)。
例如:
java -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:OldSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m Test
查看内存:
或者:
java -Xms1024m -Xmx1024m -Xmn512m -XX:PermSize=512m -XX:MaxPermSize=512m Test
结果:
在JDK1.8中,取消了PermGen,取而代之的是Metaspace,所以PermSize和MaxPermSize参数失效,取而代之的是元空间:
例如下面配置设置元空间的大小和最大大小:(等同于JDK7的永久区)
liqiang@root MINGW64 ~/Desktop/新建文件夹 (2) $ javac Test.java liqiang@root MINGW64 ~/Desktop/新建文件夹 (2) $ java -Xms1024m -Xmx1024m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m Test
查看参数:
C:\Users\liqiang>jmap -heap 2381712 Attaching to process ID 2381712, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.121-b13 using thread-local object allocation. Parallel GC with 4 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 1073741824 (1024.0MB) NewSize = 357564416 (341.0MB) MaxNewSize = 357564416 (341.0MB) OldSize = 716177408 (683.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 536870912 (512.0MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 536870912 (512.0MB) G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 268435456 (256.0MB) used = 10737448 (10.240028381347656MB) free = 257698008 (245.75997161865234MB) 4.000011086463928% used From Space: capacity = 44564480 (42.5MB) used = 0 (0.0MB) free = 44564480 (42.5MB) 0.0% used To Space: capacity = 44564480 (42.5MB) used = 0 (0.0MB) free = 44564480 (42.5MB) 0.0% used PS Old Generation capacity = 716177408 (683.0MB) used = 0 (0.0MB) free = 716177408 (683.0MB) 0.0% used 1597 interned Strings occupying 146680 bytes.
补充:查看JVM参数的方法
第一种:jps+jmap
C:\Users\Administrator>jps 16740 Main 8388 Jps 4248 11564 Server 11884 NetworkServerControl C:\Users\Administrator>jmap -heap 11564 Attaching to process ID 11564, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.121-b13 using thread-local object allocation. Parallel GC with 4 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 2155872256 (2056.0MB) NewSize = 718274560 (685.0MB) MaxNewSize = 718274560 (685.0MB) OldSize = 1437597696 (1371.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 536870912 (512.0MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 536870912 (512.0MB) G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 538968064 (514.0MB) used = 132464928 (126.32839965820312MB) free = 406503136 (387.6716003417969MB) 24.577509661129014% used From Space: capacity = 89653248 (85.5MB) used = 89640016 (85.48738098144531MB) free = 13232 (0.0126190185546875MB) 99.98524091397113% used To Space: capacity = 89653248 (85.5MB) used = 0 (0.0MB) free = 89653248 (85.5MB) 0.0% used PS Old Generation capacity = 1437597696 (1371.0MB) used = 75478984 (71.98236846923828MB) free = 1362118712 (1299.0176315307617MB) 5.250355103518475% used 61705 interned Strings occupying 7127136 bytes.
第二种:用JDK自带的工具 jconsole或者jvisualvm:
补充:32位windows平台对每个进程能管理的内存是有限制的,限制是2GB
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】