jvm参数

Posted fpsfzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jvm参数相关的知识,希望对你有一定的参考价值。

在主函数中通过下如下方法实时打印没创建一个对象后查看内存情况:

    private static void printRuntimeMemoryInfo(String str)
    {
        System.out.println("================= " + str +  " ================");
        Runtime runtime = Runtime.getRuntime();
        System.out.println("max memory: " + runtime.maxMemory());
        System.out.println("free memory: " + runtime.freeMemory());
        System.out.println("tatal memory: " + runtime.totalMemory());
    }

注:

    当前进程设置的参数为-Xms5m -Xmx10m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags

程序运行后的结果如下:

技术分享图片
 1 -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
 2 ======================= 初始状态 ==============================
 3 max memory: 10158080
 4 free memory: 5175632
 5 tatal memory: 6094848
 6 ======================= b1 ==============================
 7 max memory: 10158080
 8 free memory: 4651328
 9 tatal memory: 6094848
10 [GC (Allocation Failure) [DefNew: 1409K->191K(1856K), 0.0011437 secs] 1409K->1070K(5952K), 0.0011870 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
11 ======================= b2 ==============================
12 max memory: 10158080
13 free memory: 4441040
14 tatal memory: 6094848
15 ======================= b3 ==============================
16 max memory: 10158080
17 free memory: 3916736
18 tatal memory: 6094848
19 ======================= b4 ==============================
20 max memory: 10158080
21 free memory: 3392432
22 tatal memory: 6094848
23 [GC (Allocation Failure) [DefNew: 1760K->0K(1856K), 0.0010500 secs] 2639K->2606K(5952K), 0.0010660 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
24 ======================= b5 ==============================
25 max memory: 10158080
26 free memory: 2867912
27 tatal memory: 6094848
28 ======================= b6 ==============================
29 max memory: 10158080
30 free memory: 2343608
31 tatal memory: 6094848
32 ======================= b7 ==============================
33 max memory: 10158080
34 free memory: 1819304
35 tatal memory: 6094848
36 [GC (Allocation Failure) [DefNew: 1568K->0K(1856K), 0.0015055 secs][Tenured: 4142K->4142K(4612K), 0.0013368 secs] 4175K->4142K(6468K), [Metaspace: 2714K->2714K(1056768K)], 0.0028898 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
37 ======================= b8 ==============================
38 max memory: 10158080
39 free memory: 5336216
40 tatal memory: 10158080
41 ======================= b9 ==============================
42 max memory: 10158080
43 free memory: 4811912
44 tatal memory: 10158080
45 ======================= b10 ==============================
46 max memory: 10158080
47 free memory: 4287608
48 tatal memory: 10158080
49 ======================= b11 ==============================
50 max memory: 10158080
51 free memory: 3763304
52 tatal memory: 10158080
53 ======================= b12 ==============================
54 max memory: 10158080
55 free memory: 3239000
56 tatal memory: 10158080
57 [GC (Allocation Failure) [DefNew: 2614K->0K(3072K), 0.0011039 secs] 6756K->6703K(9920K), 0.0011251 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
58 ======================= b13 ==============================
59 max memory: 10158080
60 free memory: 2714440
61 tatal memory: 10158080
62 ======================= b14 ==============================
63 max memory: 10158080
64 free memory: 2190136
65 tatal memory: 10158080
66 ======================= b15 ==============================
67 max memory: 10158080
68 free memory: 1665832
69 tatal memory: 10158080
70 ======================= b16 ==============================
71 max memory: 10158080
72 free memory: 1113336
73 tatal memory: 10158080
74 ======================= b17 ==============================
75 max memory: 10158080
76 free memory: 589032
77 tatal memory: 10158080
78 [GC (Allocation Failure) [DefNew: 2641K->2641K(3072K), 0.0000071 secs][Tenured: 6702K->6702K(6848K), 0.0015729 secs] 9344K->9263K(9920K), [Metaspace: 2716K->2716K(1056768K)], 0.0016249 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
79 [Full GC (Allocation Failure) [Tenured: 6702K->6688K(6848K), 0.0019453 secs] 9263K->9249K(9920K), [Metaspace: 2716K->2716K(1056768K)], 0.0019585 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
80 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
81     at com.shif.test.JvmMemaryTest.main(JvmMemaryTest.java:80)
82 Heap
83  def new generation   total 3072K, used 2669K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
84   eden space 2752K,  97% used [0x00000000ff600000, 0x00000000ff89b760, 0x00000000ff8b0000)
85   from space 320K,   0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
86   to   space 320K,   0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
87  tenured generation   total 6848K, used 6688K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
88    the space 6848K,  97% used [0x00000000ff950000, 0x00000000fffd81e0, 0x00000000fffd8200, 0x0000000100000000)
89  Metaspace       used 2746K, capacity 4486K, committed 4864K, reserved 1056768K
90   class space    used 305K, capacity 386K, committed 512K, reserved 1048576K
View Code

 

通过运行结果可以得出以下结论:

1 当前进程能申请到的的最大内存为maxMemory();

2 totalMemory()返回的是我们程序当前已经申请到的内存大小;

3 freeMemorm()返回的是已经申请到的内存还有多少没有用;

(以上数据会有一些误差)

 

以上是关于jvm参数的主要内容,如果未能解决你的问题,请参考以下文章

分析定位占用CPU资源高的JVM线程

如何设置myeclipse的jvm启动参数

片段(Java) | 机试题+算法思路+考点+代码解析 2023

xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException

Visual Studio 自定义代码片段在方法定义的参数列表中不起作用

JVM 运行参数 & 代码监控