Mostrando las entradas con la etiqueta memoria. Mostrar todas las entradas
Mostrando las entradas con la etiqueta memoria. Mostrar todas las entradas

15 de septiembre de 2011

¿Cuánta memoria usa cada clase en Java?

Las siguientes herramientas nos ayudarán a diagnosticar en qué se consume la memoria heap en la máquina virtual de Java: jmap y jhat, las cuales están incluidas en el JDK.

Heap histogram

The jmap command will help you get a heap histogram that shows the per-class statistics, including the total number of instances and the total number of bytes occupied by the instances of each class.

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/#Heap_Histogram

$ <JDK>/bin/jmap -histo:live <pid>
num   #instances    #bytes  class name
--------------------------------------
  1:    100000    41600000  [LMemLeak$LeakingClass;
  2:    100000     2400000  MemLeak$LeakingClass
  3:     12726     1337184  <constMethodKlass>
  4:     12726     1021872  <methodKlass>
  5:       694      915336  [Ljava.lang.Object;
  6:     19443      781536  <symbolKlass>
  7:      1177      591128  <constantPoolKlass>
  8:      1177      456152  <instanceKlassKlass>
  9:      1117      393744  <constantPoolCacheKlass>
 10:      1360      246632  [B
 11:      3799      238040  [C
 12:     10042      160672  MemLeak$FinalizableObject
 13:      1321      126816  java.lang.Class
 14:      1740       98832  [S
 15:      4004       96096  java.lang.String
 < more .....>

Heap dump & analysis

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/#Heap_Dump
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/#Heap_Analysis

You can use the jmap command to get a heap dump with this command line:

$ <JDK>/bin/jmap -dump:live,file=heap.dump.out,format=b <pid>

You can use the jhat command to do the heap analysis and determine which references are keeping the leak suspect alive:

$ <JDK>/bin/jhat heap.dump.out

This tool supports a number of queries including the following:
  • Show all reference paths from the root set to a given object. This is particularly useful for finding memory leaks. 
  • Show the instance counts for all classes. 
  • Show the heap histogram including the instance counts and sizes for all classes. 
  • Show the finalizer summary.