java使用system.gc()内存(堆栈)回收

1:window窗口可以使用cmd命令jconsole,弹出

在这里插入图片描述

注意:不可以使用debug模式,可以使用代码现场睡眠模式测试,启动main方法后,在本地进程选择相应的执行类

public class Main2 {
    
    
    @SneakyThrows
    public static void main(String[] args) {
    
    
        if (true) {
    
    
            Thread.sleep(5000);
            byte[] placeHolder = new byte[64 * 1024 * 1024];
            System.out.println(placeHolder.length / 1024);
            Thread.sleep(5000);
        }
        System.gc();
        Thread.sleep(2000);
    }
}

注意:在执行System.gc()没存没有进行回收,因为有新建对象在,内存在第12秒后全部回收,是因为整个main方法执行结束了

在这里插入图片描述

public class Main2 {
    
    
    @SneakyThrows
    public static void main(String[] args) {
    
    
        if (true) {
    
    
            Thread.sleep(5000);
            byte[] placeHolder = new byte[64 * 1024 * 1024];
            System.out.println(placeHolder.length / 1024);
            Thread.sleep(5000);
            placeHolder=null;
        }
        System.gc();
        Thread.sleep(2000);
    }

注意:内存在第10秒后就差不多全部回收,是因为placeHolder=null,剩下的就是main方法全部结束后回收了

在这里插入图片描述

2:使用java命令启动,如果类里面有包含包路径,就切换到类里面包路径的根目录执行,如下

package org.jeecg.modules.system.service.impl;

import lombok.SneakyThrows;

/**
 * @Author xu
 * @create 2023/7/13 01
 */
public class Main2 {
    
    
    @SneakyThrows
    public static void main(String[] args) {
    
    
        if (true) {
    
    
            Thread.sleep(5000);
            byte[] placeHolder = new byte[64 * 1024 * 1024];
            System.out.println(placeHolder.length / 1024);
            Thread.sleep(5000);
            placeHolder=null;
        }
        System.gc();
        Thread.sleep(2000);
    }
}

编译Main2.java为Main2.class文件

G:\hdxProject\java\org\jeecg\modules\system\service\impl>javac Main2.java
G:\hdxProject\java>

启动Main2类

G:\hdxProject\java>java org/jeecg/modules/system/service/impl/Main2
65536

G:\hdxProject\java>

启动Main2类并打印内存回收信息

G:\hdxProject\java>java -XX:+PrintGC org/jeecg/modules/system/service/impl/Main2
65536
[GC (System.gc())  69447K->66392K(373248K), 0.0203310 secs]
[Full GC (System.gc())  66392K->66202K(373248K), 0.0147270 secs]

G:\hdxProject\java>

启动Main2类并打印内存回收详细信息

G:\hdxProject\java>java -XX:+PrintGCDetails org/jeecg/modules/system/service/impl/Main2
65536
[GC (System.gc()) [PSYoungGen: 69447K->784K(113664K)] 69447K->66328K(373248K), 0.0200134 secs] [Times: user=0.00 sys=0.02, real=0.02 secs]
[Full GC (System.gc()) [PSYoungGen: 784K->0K(113664K)] [ParOldGen: 65544K->66202K(259584K)] 66328K->66202K(373248K), [Metaspace: 2735K->2735K(1056768K)], 0.0047035 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 PSYoungGen      total 113664K, used 978K [0x0000000741980000, 0x0000000749800000, 0x00000007c0000000)
  eden space 97792K, 1% used [0x0000000741980000,0x0000000741a74920,0x0000000747900000)
  from space 15872K, 0% used [0x0000000747900000,0x0000000747900000,0x0000000748880000)
  to   space 15872K, 0% used [0x0000000748880000,0x0000000748880000,0x0000000749800000)
 ParOldGen       total 259584K, used 66202K [0x0000000644c00000, 0x0000000654980000, 0x0000000741980000)
  object space 259584K, 25% used [0x0000000644c00000,0x0000000648ca6970,0x0000000654980000)
 Metaspace       used 2741K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

G:\hdxProject\java>

3:设置idea执行System.gc()打印内存回收信息

在这里插入图片描述
在这里插入图片描述

设置idea执行System.gc()打印内存详细回收信息

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_19891197/article/details/131696552