【148】如何解决Linux上Java的-Xmx参数失效的问题

环境

操作系统:Linux CentOS 7

Java版本:jdk1.8.0_191

问题复现

我写了个 start.sh 文件来启动Java程序,sh文件里面带有Java启动参数。start.sh 文件内容如下:

nohup /opt/jdk1.8.0_191/bin/java -jar your_jar_file.jar -server -Xms512m -Xmx512m -Xmn170m > /data0/out/outfile.out  --spring.profiles.active=test --server.port=80 &
echo "start"

然后我线用 ps -ef|grep your_jar_file 查询到Java程序的进程ID,再用jmap -heap 命令查看JVM参数

/$ jmap -heap 24653
Attaching to process ID 24653, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.191-b12

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 4164943872 (3972.0MB)
   NewSize                  = 87031808 (83.0MB)
   MaxNewSize               = 1388314624 (1324.0MB)
   OldSize                  = 175112192 (167.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 846200832 (807.0MB)
   used     = 712246456 (679.2511520385742MB)
   free     = 133954376 (127.74884796142578MB)
   84.16990731580846% used
From Space:
   capacity = 22020096 (21.0MB)
   used     = 21963048 (20.945594787597656MB)
   free     = 57048 (0.05440521240234375MB)
   99.74092755998883% used
To Space:
   capacity = 25690112 (24.5MB)
   used     = 0 (0.0MB)
   free     = 25690112 (24.5MB)
   0.0% used
PS Old Generation
   capacity = 299892736 (286.0MB)
   used     = 90683136 (86.482177734375MB)
   free     = 209209600 (199.517822265625MB)
   30.238523683347903% used

35761 interned Strings occupying 3503720 bytes.

注意 MaxHeapSize 这一行,是 3972 M,根本不是我设置Xmx的512M,MaxNewSize 是1324 M,也根本不是Xmn设置的170M。

我们也可以用 jcmd VM.flags 看一下JVM参数:

/root$ jcmd 24653 VM.flags
24653:
-XX:CICompilerCount=3 -XX:InitialHeapSize=262144000 -XX:MaxHeapSize=4164943872 -XX:MaxNewSize=1388314624 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=87031808 -XX:OldSize=175112192 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 

参数单位是字节(byte),可以看出 -XX:MaxHeapSize 和 -XX:NewSize 都不是我们想设置的值。

修改方案

后来我发现应该是参数位置不对,-Xmx -Xms -Xmn 参数应该放在jar文件名之前,变成如下样子:

start.sh

nohup /opt/jdk1.8.0_191/bin/java -jar your_jar_file.jar -server -Xms512m -Xmx512m -Xmn170m > /data0/out/outfile.out  --spring.profiles.active=test --server.port=80 &
echo "start"

重启程序,然后用 jmap 看一下设置:

/$ jmap -heap 7132
Attaching to process ID 7132, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.191-b12

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 536870912 (512.0MB)
   NewSize                  = 178257920 (170.0MB)
   MaxNewSize               = 178257920 (170.0MB)
   OldSize                  = 358612992 (342.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 112721920 (107.5MB)
   used     = 21211560 (20.228919982910156MB)
   free     = 91510360 (87.27108001708984MB)
   18.817599984102472% used
From Space:
   capacity = 33030144 (31.5MB)
   used     = 14072064 (13.420166015625MB)
   free     = 18958080 (18.079833984375MB)
   42.60370163690476% used
To Space:
   capacity = 32505856 (31.0MB)
   used     = 0 (0.0MB)
   free     = 32505856 (31.0MB)
   0.0% used
PS Old Generation
   capacity = 358612992 (342.0MB)
   used     = 172781264 (164.7770538330078MB)
   free     = 185831728 (177.2229461669922MB)
   48.18042509737071% used

55733 interned Strings occupying 6060800 bytes.

可以看到 MaxHeapSize、MaxNewSize 都已经改成 512M、170M了。

用 jcmd VM.flags 查看:

/$ jcmd 7132 VM.flags
7132:
-XX:CICompilerCount=3 -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=178257920 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=178257920 -XX:OldSize=358612992 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 

可以看出来 -XX:MaxHeapSize 和 -XX:MaxNewSize 都已经改过来了。

你可能感兴趣的:(JAVA,LINUX命令,JVM)