Short jhat tutorial: diagnosing OutOfMemoryError by example

转自: http://petermodzelewski.blogspot.com/2013/06/short-jhat-tutorial-diagnosing.html

Last time we‘ve learned what can be the reason of OutOfMemoryErrors and what tool-chain can we use to diagnose it. Today we will learn by example how to use the most important one: jhat.
I‘ve prepared a sample project for this exercise, which can be cloned from github. The code is really simple and its  problem is obvious, but this simplicity will make it easy to get to know jhat.
First, we need to run our program. We will use small heap size, for two reasons:

  1. Program will throw the exception faster
  2. jhat will start more quickly, as the heap dump will be smaller

?


1

2

3

4

5

6

7

$ git clone https://github.com/petermodzelewski/snippets.git

$ cd snippets/OutOfMemoryErrorGenerator/

$ mvn package

$ java -Xmx128m -Xms128m -jar target/OutOfMemoryErrorGenerator-1.0.jar

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

        at pl.keyer.oome.generator.Sleepyhead(Sleepyhead.java:6)

        at pl.keyer.oome.generator.App.main(App.java:11)

We can notice, that the program is still running. We will need another console to run jhat. We are using the following commands:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

$ jps -l

752 target/OutOfMemoryErrorGenerator-1.0.jar

4480 sun.tools.jps.Jps

$ jmap -dump:file=dump.map 752

$ jhat -port 7401 dump.map

Reading from dump.map...

Dump file created Sat Jun 01 23:25:55 CEST 2013

Snapshot read, resolving...

Resolving 561438 objects...

Chasing references, expect 112 dots................................................................................................................

Eliminating duplicate references................................................................................................................

Snapshot resolved.

Started HTTP server on port 7401

Server is ready.

Important notes about that process:

  • All commands must be executed by the same user: the java process owner
  • The "expect X dots" message is not a joke. While processing bigger heap dumps one can check the number of dots there in editor to see the progress, as it can take quite a while to process such a file.
  • When processing bigger dumps one must watch heap size of jhat itself. This depends on the case, but to be safe (provided with enough resources) jhat should have 2-4 times more heap size, than process heap it will diagnose. If memory size for jhat is too small it will just crush after using it and the process will need to be repeated with bigger amount of memory. For example to provide jhat with 4 gigs the command will be:

    ?


    1

    $ jhat -port 7401 -J-mx4G dump.map

  • Diagnosed process may be terminated after dumping heap with jmap.
  • Obviously jhat can be run on any machine where the dump will be present. On many occasions developers choose to zip the dump and move the debug process to machine more accessible for them and with enough ram.

After executing the commands we can visit http://localhost:7401/

When facing with jhat You will quickly realize that this tool is from times, where such tools were designed without consideration of prettiness or usability. This tutorial will show how to navigate it in most cases - all it‘s features are cool and necessary, but everyday programmer will use only subset of them to quickly diagnose where the OOME came from.
jhat main page can be divided into sections:

  1. List of all classes in your program (excluding platform - that is, all that is not from standard library). This list is normally really long and in most cases it is not necessary. Normally You will scroll down to "Other Queries" section right away.
  2. More options for listing classes
  3. Bread and butter of memory debugging, we will use them in a moment
  4. More tools for debugging, but not as helpfull as section 3.
    • Heap histogram is sometimes useful to compare quantity vs size of objects
    • When you become jhat ninja, you sometimes could use OQLto diagnose the application. It is a SQL-like language for searching heap and calculating it‘s statistics.

Firstly, lets see heap histogram.

This view illustrates very important fact, that jhat does not compute "deep" object size. That‘s why on top of our process‘ memory consumption histogram we see class [B which is really an array of bytes:byte[]. Array of bytes is often on top of such histogram and what‘s more - in most cases it doesn‘t mean that there is a problem. For example, if your program processes lot‘s of strings it will naturally have lot of byte arrays, as each string object contains reference to some byte array. Similar problem will manifest when we follow the "Show instance counts for all classes (including platform)".

That view is very similar to histogram sorted by quantity. Normally during OOMR we will look for "our" classes. We will need to exclude platform classes to easily detect abnormalities in our classes quantity (or size). A good start is to follow "Show instance counts for all classes (excluding platform)".

Things to look: unnaturally big numbers of objects of some class. In our example the number of workers. Our example illustrates the common problem, when producer creates tasks faster, than consumer handles them, and the producer - consumer queue will not block after limit is reached.

Unfortunately in most cases it is not as easy. Diagnosing that objects of some class are eating too much memory is one thing. Diagnosing why and where are they allocated is another. To do so, we need to track the objects references to something "active", for example thread, controller, etc. When given that "active" object we can than analyze the algorithm and find out why so many objects are created.

To illustrate such process lets track class [B references.

jhat enables going through references as long as one needs. Unfortunately when you click on class "page" it will display all it‘s instances. To dodge that time consuming view, you can copy link of the class page (for example from histogram view) and construct reference by Type view link. For example class [B page can have the following url:
http://localhost:7401/class/0x3881d790
so the reference by type summery will have url as follow:
http://localhost:7401/refsByType/0x3881d790

Those methods normally are all you need to detect the memory trouble in the process. Additionally in case you are working with application container (like tomcat) and having problems to see what application (or pool) is leaking the objects, you should diagnose the Classloader section of class page:

That‘s it. All you need to know about jhat to start your own memory problem debug. Hope it‘ll help.

 

One last tip: Many developers after solving their memory problem while running new application version, are taking heap dump and running jhat with it - just in case. They are often terrified that despite their efforts object count is still grand but somehow OOME is not appearing. Of course they were so focused on fighting the memory leakage that they‘ve forget how GC works. Remember:Always trigger GC collection (for example with visual vm) to clean old gen from unnecessary objects before taking the dump and analyzing it with jhat.

时间: 2024-10-07 01:08:16

Short jhat tutorial: diagnosing OutOfMemoryError by example的相关文章

PBS命令和使用

PBS是公开源代码的作业管理系统,在此环境下运行,用户不需要指定程序在哪些节点上运行,程序所需的硬件资源由PBS管理和分配. PBS(Portable Batch System)是由NASA开发的灵活的批处理系统.它被用于集群系统.超级计算机和大规模并行系统.PBS主要有如下特征: 易用性:为所有的资源提供统一的接口,易于配置以满足不同系统的需求,灵活的作业调度器允许不同系统采用自己的调度策略. 移植性:符合POSIX 1003.2标准,可以用于shell和批处理等各种环境. 适配性:可以适配与

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript

斯坦福大学机器学习公开课 ---Octave Tutorial Transcript Prompt (命令窗口提示符)can be changed with the command PS1('>> '). Transcript 1  Basics 1.1 Basic algebra in Octave Elementary +; -; *; / ;            %arithmetic operations. == ; ~=;&&; ||; xor ;  % logic

教程 1:让我们通过例子来学习(Tutorial 1: Let’s learn by example)

通过这第一个教程,我们将引导您从基础完成创建简单的带有注册表单的应用. 我们也将解释框架行为的基本方面.如果您对Phalcon的自动代码生成工具有兴趣, 您可以查看 developer tools. 确认安装(Checking your installation)? We'll assume you have Phalcon installed already. Check your phpinfo() output for a section referencing "Phalcon"

了解OutOfMemoryError异常 - 深入Java虚拟机读后总结

JVM中的异常发生 Java虚拟机规范中除了程序计数器外,其他几个运行时区域都有发生OutOfMemoryError异常的可能. 本章笔记通过代码来验证Java虚拟机规范中描述的各个运行时区域存储的内容.以及在以后遇到实际的内存溢出异常时,能根据异常的信息快速判断是哪个区域出现的内存溢出.怎样的代码可能会导致这些区域的内存溢出.以及这些问题该如何处理. Java堆溢出:Java堆用于存储对象实例,只要不断的创建对象,并且保证GC Roots到对象之间有可达路径避免垃圾回收机制清除对象,就会在对象

JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解

现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 ...... 这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),但能够理解并解决这些问题是Java程序员进阶的必备要求.本文将对一些常用的JVM性能调优监控工具进行介绍,希望能起抛砖引玉之用.本文参考了网上很多资料,难以一一列举,在此对这些资料的

jmap,jhat分析内存

分析JAVA Application的内存使用时,jmap是一个很实用的轻量级工具.使用jmap可以查看heap空间的概要情况,粗略的掌握heap的使用情况.也可以生成heapdump文件,再使用jhat通过web浏览器具体分析内容中的对象和数据. jmap是JDK自带的一个工具,非常小巧方便,其支持参数如下: -heap      打印heap空间的概要,这里可以粗略的检验heap空间的使用情况. jmap -heap PID 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution

Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz3QDWa3Zqi Tomcat web server often suffers from java.lang.OutOfMemoryError: PermGen space whenever you deploy and undeploy your web application coupl

JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解

JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jstack.jmap.jhat.jstat等小巧的工具,本博客希望能起抛砖引玉之用,让大家能开始对JVM性能调优的常用工具有所了解. 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 ...... 这些问题在日常开发中可能被很多人忽视(比如有的

HotSpot中OutOfMemoryError解析

在JVM中内存一共有3种:Heap(堆内存),Non-Heap(非堆内存) [3]和Native(本地内存). [1] 堆内存是运行时分配所有类实例和数组的一块内存区域.非堆内存包含方法区和JVM内部处理或优化所需的内存,存放有类结构(如运行时常量池.字段及方法结构,以及方法和构造函数代码).本地内存是由操作系统管理的虚拟内存.当一个应用内存不足时就会抛出java.lang.OutOfMemoryError 异常. [1] 问题 表象 诊断工具 内存不足 OutOfMemoryError Jav