jvm优化之:OOM(out of memory)内存溢出

内存溢出

复制代码
 注意内存溢出不是内存泄漏!!这里主要是介绍如何用jdk自带的jmap工具导出进程堆空间快照。
  1. 内存溢出

    Out Of Memory,是指申请的堆内存空间不够用了,比如:你申请了10M空间,但是你要放12M的东西进去;

  2. 内存泄漏

    Memory Leak,是指你的堆空间一直有对象不能被GC清理掉,但是你还要放对象进去,还在向堆申请放对象的空间,此时就会报 Memory Leak。

  3. 这里主要演示内存溢出 : 模拟代码

java 复制代码
package com.distributed.lock.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 1. -Xms30m -Xmx=30m -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:\99.hprof
 2. 模拟OOM
 */
public class OOMTest {

    public static void main(String[] args) {

        System.out.println("演示开始:>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        //先向堆空间申请一块内存区域,区域的大小默认是ArrayList的长度
        List<int[]> arr = new ArrayList<>(10);

        for (int i = 0; i < 10000; i++) {
            //不断的新创建对象,对象大小为1M
            int[] ints = new int[1024*1024];
            //一直往ArrayList集合添加
            arr.add(ints);
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("演示结束:>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}
  1. 异常信息

  2. 导出堆空间快照(dump) :

    先看jmap语法规则:

java 复制代码
C:\Windows\system32>jmap -help
Usage: //语法规则,使用方式
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of: //参数选项介绍
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live" 
                         suboption is specified, only count live objects //只是当前时间节点活着的对象
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

将堆空间信息以标准格式导出到文件:

java 复制代码
jmap -dump:format=b,file=d:\7.hprof 14876

将堆空间存活对象的信息以标准格式导出到文件:

java 复制代码
jmap -dump:live,format=b,file=d:\7.hprof 14876

显示当前时间节点堆内存信息:

java 复制代码
jmap -heap 14876 >d:\a.txt  // 17924 是进程号,输出到d盘的a.txt文件

直接控制台查看堆空间信息:

java 复制代码
C:\Windows\system32>jmap -heap 14876
Attaching to process ID 14876, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10

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

Heap Configuration: //堆空间的配置信息
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 8550088704 (8154.0MB)
   NewSize                  = 178257920 (170.0MB)
   MaxNewSize               = 2850029568 (2718.0MB)
   OldSize                  = 356515840 (340.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://Eden区
   capacity = 134217728 (128.0MB) 
   used     = 123984136 (118.24048614501953MB)
   free     = 10233592 (9.759513854980469MB)
   92.37537980079651% used
From Space: //Survivor from区,或者:S0
   capacity = 22020096 (21.0MB)
   used     = 0 (0.0MB)
   free     = 22020096 (21.0MB)
   0.0% used
To Space: //Survivor to区,或者:S1
   capacity = 22020096 (21.0MB)
   used     = 0 (0.0MB)
   free     = 22020096 (21.0MB)
   0.0% used
PS Old Generation //old区,或者:老年代
   capacity = 356515840 (340.0MB)
   used     = 0 (0.0MB)
   free     = 356515840 (340.0MB)
   0.0% used

3196 interned Strings occupying 261920 bytes.

使用的:ParallelGC垃圾收集器

直接查看进程堆空间历史对象信息

java 复制代码
jmap -histo 15440
相关推荐
264玫瑰资源库1 小时前
问道数码兽 怀旧剧情回合手游源码搭建教程(反查重优化版)
java·开发语言·前端·游戏
pwzs1 小时前
Java 中 String 转 Integer 的方法与底层原理详解
java·后端·基础
东阳马生架构1 小时前
Nacos简介—2.Nacos的原理简介
java
普if加的帕1 小时前
java Springboot使用扣子Coze实现实时音频对话智能客服
java·开发语言·人工智能·spring boot·实时音视频·智能客服
爱喝一杯白开水2 小时前
SpringMVC从入门到上手-全面讲解SpringMVC的使用.
java·spring·springmvc
王景程2 小时前
如何测试短信接口
java·服务器·前端
zhang23839061543 小时前
IDEA add gitlab account 提示
java·gitlab·intellij-idea·idea
牛马baby3 小时前
Java高频面试之并发编程-07
java·开发语言·面试
卓怡学长3 小时前
w304基于HTML5的民谣网站的设计与实现
java·前端·数据库·spring boot·spring·html5
YONG823_API3 小时前
深度探究获取淘宝商品数据的途径|API接口|批量自动化采集商品数据
java·前端·自动化