转自: https://blog.codecentric.de/en/2008/07/memory-analysis-part-1-obtaining-a-java-heapdump/
For troubleshooting Java memory leaks and high memory usage problems, the heapdump is one of the most important analysis features. The advantage of heapdumps is that they can be produced in productive environments – the place where the problems most frequently occur. All current Java Virtual Machines support the generation of heapdumps without the need of additional tools.
In this blog series I will show you how to analyze and fix memory problems in production . I will also provide a list of common antipatterns and memory problems.
The first part of this series deals with the vital task of heapdump generation – the main precondition for a successful analysis. The various JVM manufacturers (SunIBMBEA) have different tools and formats to dump the heap of the JVM – this blog therefore focus on the implementation of Sun. The Sun Java Virtual Machine contains several options and tools to create a heapdump:
– Automatically when a java.lang.OutOfMemoryError occurs

– With the command line tool jmap

– By using a provided MBean (Java Management Extension – JMX) and the tooljconsole

Of course, there is the possibility to use the Java Virtual Machine Tool Interface (JVMTI) to produce a dump – but therefore you would have to implement an agent in C. Many Profiling Tools (like JProfiler) provide a JVMTI agent to create and evaluate a heapdump with a GUI.

To automatically generate a heap dump when an OutOfMemoryError is thrown you have to provide this JVM command line parameter:

– XX:+HeapDumpOnOutOfMemoryError

The parameter causes the JVM to dump a HPROF headump to the current directory if an OutOfMemoryError occurs. The name of the dump is by conventionjava_pid.hprof. To specify the directory and the name of the file by yourself, you can add the parameter -XX:HeapDumpPath=path_to_file to the JVM command line options.

The automatic production of dumps with these parameters is not always useful. In some situations you want to produce a heapdump at any given time during application execution. In this case Java version 1.4.2_09, 1.5.x and 1.6.x provide the tool jmap. A HPROF heapdump can be requested by executing the following command:

jmap -dump:file=path_to_file java_process_id.

The provided Java process id determines which local JVM should be dumped. The process Id can be determined with the JVM Tool jps (Note: The jmap tool is not available on every platform and JVM version). You can alternatively use the JVM parameter -XX:+HeapDumpOnCtrlBreak and send a SIGQUIT signal (-3 kill for Unix and Ctrl-Break for Windows) to the running Java process – the signal will also create a heapdump without aborting the JVM.

With Java 6, Sun introduced a JMX MBean which provides methods for generating a heapdump. To create a heapdump via JMX you first start the integrated JMX console with the command jconsole and connect it to the corresponding JVM. For a local connection you don’t need any additional configuration of the JVM – for a connection to a remote machine you have to configure JMX correctly.

You can use the MBean Explorer of jconsole to find the correct MBean within the JVM. The MBean com.sun.management.HotSpotDiagnostic contains the alluded method dumpHeap (String, boolean). With the help of the first method parameter you can set the path and the name of the Heapdump. The screenshot shows the view of the MBean. Pressing on the dumpHeap button will create a heapdump with the given name.

The next part of this series will get into more details how to analyse heapdumps and find memory leaks and common memory antipatterns.

Information about heapdump generation with the IBM JVM can be found in the IBM JVM Diagnosis Documentation.

Information about BEA JRockit and the JRockit Memory Leak Detector can be found in the JRockit Dokumentation.

Memory Analysis Part 1 – Obtaining a Java Heapdump的更多相关文章

  1. Diagnosing out of memory errors and memory leaks 内存泄露实例 C Java JavaScript 内存泄露

    小结: 1. 数据库连接池. JDBC语句和结果对象必须显式地关闭. 2. 电梯到目标楼层后地址是否被释放 When a button is pressed: Get some memory, whi ...

  2. Memory Analysis环境安装

    安装MAT(MAT在eclipse的页面:http://www.eclipse.org/mat/downloads.php) 显示饼图的时候,需要安装BIRT Chart Engine插件,通过Ins ...

  3. Automated Memory Analysis

    catalogue . 静态分析.动态分析.内存镜像分析对比 . Memory Analysis Approach . volatility: An advanced memory forensics ...

  4. Eclipse Memory Analysis进行堆转储文件分析

    生成堆转储文件 新建项目,设置Eclispe Java堆的大小: (1)限制Java堆大小:将最小值 -Xms参数与最大值-Xmx参数设置一样可避免堆的扩展         -Xmx20m -Xms2 ...

  5. jmap命令(Java Memory Map)(转)

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  6. JDK内置工具之一——JMap(java memory map)

    1.介绍 打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其数量). 可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本.使用方法 j ...

  7. 五、jdk工具之jmap(java memory map)、 mat之四--结合mat对内存泄露的分析、jhat之二--结合jmap生成的dump结果在浏览器上展示

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  8. On Memory Leaks in Java and in Android.

    from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mea ...

  9. DTrace to Troubleshoot Java Native Memory Problems

    How to Use DTrace to Troubleshoot Java Native Memory Problems on Oracle Solaris 11 Hands-On Labs of ...

随机推荐

  1. gif屏幕录像软件

    ScreenToGif:http://screentogif.codeplex.com       备份下载地址:http://flask.pub/screentogif2.zip 本站连接:http ...

  2. CentOS7.6安装Redis

    官网下载地址:https://redis.io/download 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进制包:wget htt ...

  3. array,vector对象 数组越界检测

  4. freebsd静态路由

    FreeBSD下增进静态路由的行动 1.手工添加 # route add -net 192.168.2.0/24 192.168.1.2 2. 通过rc.conf永世 设置 # Add static ...

  5. Netty的ChannelHandler,ChannelHandlerContext,ChannelPipeline

    本小节一起学习一下ChannelHandler,ChannelHandlerContext,ChannelPipeline这三个Netty常用的组件,不探究它们的底层源码,我们就简单的分析一下用法 首 ...

  6. vue打包静态资源路径不正确的解决办法

    vue打包静态资源路径不正确的解决办法 vue项目完成打包上线的时候会碰到静态资源找不到的问题,常见的有两个 1.js,css路径不对 解决办法:打开config/index.js,将其中的asset ...

  7. UNITY 带spriterender的对象导出为prefab时主贴图丢失的BUG

    从场景导出带有sprite的对象为prefab时贴图丢失的BUG.解决方案:对场景中每个sprite重新赋一下贴图,然后导出就好了,原因不明. 补充:这个有时候是因为贴图类型不是 2D AND UI ...

  8. 什么是web资源????

    所谓 web 资源即放在 Internet 网上供外界访问的文件或程序,又根据它们呈现的效果及原理不同,将它们划分为静态资源和动态资源. 1. 什么是静态资源 静态资源是浏览器能够直接打开的,一个 j ...

  9. beego 自定义模板函数

    beego支持的模板函数不是很多,有时候前端展现数据的时候,要对数据进行格式化,所以要用到自定义模板函数 比如我的前端模板上有时间和模板大小这2个数据,原始数据都是int的时间戳和byte单位的数据, ...

  10. Android开发实战之ViewPager实现向导界面

    当我们更新应用,或者第一次进入应用时都会有一个向导界面,介绍这个app的内容和使用方式. 如果你细心你会发现其实这就是个viewpager,本篇博文将介绍应用的向导界面是如何制作的.希 望本篇博文对你 ...