java code to java heap - en français

38
© 2012 IBM Corporation From Java Code to Java Heap Comprendre l'utilisation mémoire de votre application Chris Bailey – IBM Java Service Architect 9 th September 2012

Upload: chris-bailey

Post on 11-Nov-2014

1.289 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java Code to Java Heap - En Français

© 2012 IBM Corporation

From Java Code to Java HeapComprendre l'utilisation mémoire de votre application

Chris Bailey – IBM Java Service Architect 9th September 2012

Page 2: Java Code to Java Heap - En Français

2 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Important Disclaimers

THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.

WHILST EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.

ALL PERFORMANCE DATA INCLUDED IN THIS PRESENTATION HAVE BEEN GATHERED IN A CONTROLLED ENVIRONMENT. YOUR OWN TEST RESULTS MAY VARY BASED ON HARDWARE, SOFTWARE OR INFRASTRUCTURE DIFFERENCES.

ALL DATA INCLUDED IN THIS PRESENTATION ARE MEANT TO BE USED ONLY AS A GUIDE.

IN ADDITION, THE INFORMATION CONTAINED IN THIS PRESENTATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM, WITHOUT NOTICE.

IBM AND ITS AFFILIATED COMPANIES SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION.

NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR SHALL HAVE THE EFFECT OF:

- CREATING ANY WARRANT OR REPRESENTATION FROM IBM, ITS AFFILIATED COMPANIES OR ITS OR THEIR SUPPLIERS AND/OR LICENSORS

Page 3: Java Code to Java Heap - En Français

3 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Agenda

■ Introduction à la gestion de la mémoire

■ Anatomie d'un objet Java

■ Comprendre les Collections Java

■ Analyser votre Collection Java

Page 4: Java Code to Java Heap - En Français

4 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Introduction à la gestion de la mémoire

■ Java fonctionne comme un Systeme d'Exploitation (SE):

■ L'architecture et/ou un SE 32bits offre 4 Go d'espace d'adressage par processus– Beaucoup plus grand pour 64bits

0 GB 4 GB

0x0 0xFFFFFFFF

2 GB

0x800000000x40000000 0xC0000000

SE et C-Runtime JVM Tas Java

-Xmx

■ Une partie est utilisée par le SE et la C-Runtime■ Une partie est utilisée par le(s) tas Java

…et le reste est utilisé pour le tas “natif”

Tas Natif

■ Une partie du tas “natif” est pour la Machine Virtuelle Java (JVM)

Page 5: Java Code to Java Heap - En Français

5 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Objets Java utilisants des resources “natives”

■ Certains Objets Java utilisent des ressources du SE– Par conséquent utilisent la mémoire "native"

■ Exemple: java.lang.Thread

Tas Java Tas Natif

Page 6: Java Code to Java Heap - En Français

6 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Anatomie d'un objet Java

public class CreateInteger {public static void main(String[] args) {

Integer myInteger = new Integer(10);}

}

■ Question: Quel est la taille d'un objet de type Integer par rapport a int (par example 10) ?(sur une architecture 32bits)

(a) x1(b) x1.5(c) x2(d) x3

■ Solution: (e) x4 !!

Page 7: Java Code to Java Heap - En Français

7 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Anatomie d'un objet Java

■ Objet Metadata:– Class: pointeur sur une classe– Flags: shape, code de hashage, etc– Lock: verrou ou pointeur sur un moniteur– Size: longueur du tableau (tableau seulement)

0 32 64 96 128 160 192 224Taille/Bits

256 288 320

Pointeur Classe Flags Locks int, eg. 10

Pointeur Classe Flags Locks int, eg. 10Size

Java Objet

Array Objet

Page 8: Java Code to Java Heap - En Français

8 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

■ L'objet Integer occupe 9 fois plus de mémoire que le int !

Anatomie d'un Objet Java 64 bits

■ Objet Metadata:– Class:pointeur sur une classe – Flags:shape, code de hashage, etc– Lock: verrou ou pointeur sur un moniteur– Size: longueur du tableau (tableau seulement)

0 32 64 96 128 160 192 224Taille/Bits

256 288 320

Java Objet

Array Objet

Class pointer Flags Locks intPointeur de Classe Flags Locks int, eg. 10

Class pointer Flags Locks intSizePointeur de Classe Flags Locks int, eg. 10Size

Page 9: Java Code to Java Heap - En Français

9 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Tailles des différents types

Field Type Field size/bits32bits 64bits

Objet Tableau Objet Tableauboolean 32 8 32 8byte 32 8 32 8char 32 16 32 16short 32 16 32 16int 32 32 32 32float 32 32 32 32long 64 64 64 64double 64 64 64 64Objets 32 32 64* 64

*32bits if Compressed References / Compressed Oops enabled

Page 10: Java Code to Java Heap - En Français

10 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Compressed References et CompressedOOPs

■ Transférer une application d'une architecture 32bits à 64bits augmente l'utilisation mémoire:– Le tas Java est 70% plus grand– Le tas “Natif” est 90% plus grand

■ “Compressed References” et “Compressed Ordinary Object Pointers (Compessed Oops)”– Réduire la consommation mémoire pour 64bits– Les metadata et réferences des objets deviennent 32bits

■ En utilisant “Compressed References” ou “Compressed Oops”:– Le tas Java est 0% plus grand– Le tas “Natif” est 90% plus grand

Page 11: Java Code to Java Heap - En Français

11 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Pour les objets plus complexes

■ Exemple: java.lang.String pour “MyString”:

public static void main(String[] args) {String myString = new String("MyString");

}

■ Pour 128bits de données de type char, on utilise 480bits de mémoire x3.75 plus – Pour un seul caractère char, l'augmentation est de x24 !

0 32 64 96 128 160 192 224 256 288 320

Class pointer Flags Locks hash

Class pointer Flags Locks charMSize

java.lang.String

Taille/Bits

count offset value

charY

charS

charT

charR

charI

charN

charG char[]

Page 12: Java Code to Java Heap - En Français

12 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Collections Java

■ Chaque Collection Java offre un niveau de fonctionnalités différent, cela induit une consommation mémoire différente

■ Utiliser le mauvais type de Collection peut engranger une importante surcharge mémoire.

Plus

de

func

tion

Plu

s de

mém

oire

java.util.HashSetjava.util.HashMapjava.util.Hashtablejava.util.LinkedListjava.util.ArrayList

Page 13: Java Code to Java Heap - En Français

13 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

HashSet

■ Implémentation HashSet de l'interface Set:– “A collection that contains no duplicate elements. More formally, sets contain no pair of

elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction. “

• Java Platform SE 6 API doc

■ L'implémentation est un objet HashMap:

■ Capacité par défaut: 16

■ Taille vide: 144 octets

■ 16 octets / 128 bits en plus de HashMap

public static void main(String[] args) {HashSet myHashSet = new HashSet();

}

Page 14: Java Code to Java Heap - En Français

14 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

HashMap

■ Implémentation HashMap de l'interface Map:– “An object that maps keys to values. A map cannot contain duplicate keys; each key can

map to at most one value.“• Java Platform SE 6 API doc

■ L'implémentation est un tableau d'objet HashMap$Entry :

■ Capacité par défaut: 16■ Taille vide: 128 octets■ Mémoire utilisée: 48 octets pour la HashMap,

plus (16 + (entries * 4octets)) pour le tableau– Ainsi que la mémoire utilisée par les Objets HashMap$Entry

public static void main(String[] args) {HashMap myHashMap = new HashMap();

}

Page 15: Java Code to Java Heap - En Français

15 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

HashMap$Entry

■ Chaque HashMap$Entry a:– int KeyHash– Objet next– Objet key– Objet value

■ 32 octets par entrée clé ↔ valeur

■ Mémoire utilisée pour HashMap:– 48 octets + 36 octets pour chaque HashMap$Entry

■ Mémoire utilisée pour 10,000 d'entrée est ~360Ko

Page 16: Java Code to Java Heap - En Français

16 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Hashtable

■ Implémentation Hashtable de l'interface Map :– “This class implements a hashtable, which maps keys to values. Any non-null object can

be used as a key or as a value.“• Java Platform SE 6 API doc

■ L'implémentation est un tableau d'objets Hashtable$Entry :

■ Capacité par défaut: 11■ Taille vide: 104 octets■ Mémoire utilisée: 40 octets pour la Hashtable,

plus (16+(entries * 4octets)) pour le tableau– Ainsi que la mémoire utilisée par les objets Hashtable$Entry

public static void main(String[] args) {Hashtable myHashtable = new Hashtable();

}

Page 17: Java Code to Java Heap - En Français

17 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Hashtable$Entry

■ Chaque Hashtable$Entry a:– int KeyHash– Objet next– Objet key– Objet value

■ 32 octets par entrée clé ↔ valeur

■ Mémoire utilisée pour la Hashtable:– 40 octets + 36 octets pour chaque Hashtable$Entry

■ Mémoire utilisée pour 10,000 entrées est ~360K

Page 18: Java Code to Java Heap - En Français

18 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

LinkedList

■ L'implémentation LinkedList de l'interface List :– “An ordered collection (also known as a sequence). The user of this interface has

precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

– Unlike sets, lists typically allow duplicate elements. “• Java Platform SE 6 API doc

■ L'implémentation est une liste chainée d'objets LinkedList$Entry (ou LinkedList$Link):

■ Capacité par défaut: 1■ Taille vide: 48 octets■ Mémoire utilisée: 24 octets pour la LinkedList

– Ainsi que la mémoire utilisée par les objets LinkedList$Entry/Link

public static void main(String[] args) {LinkedList myLinkedList = new LinkedList();

}

Page 19: Java Code to Java Heap - En Français

19 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

LinkedList$Entry / Link

■ Chaque LinkedList$Entry a:– Objet précédant– Objet suivant– Objet entrée

■ 24 octets par entrée

■ Mémoire utilisée pour la LinkedList:– 24 octets, + 24 octets pour chaque LinkedList$Entry

■ Mémoire utilisée pour 10,000 entrées est ~240Ko

Page 20: Java Code to Java Heap - En Français

20 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

ArrayList

■ L'implémentation ArrayList de l'interface List :– “An ordered collection (also known as a sequence). The user of this interface has

precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

– Unlike sets, lists typically allow duplicate elements. “• Java Platform SE 6 API doc

■ L'implémentation est un tableau d'objets:

■ Capacité par défaut: 10

■ Taille vide: 88 octets

■ Mémoire utilisée: 32 octets pour l'ArrayList,plus (16 + (entries * 4octets)) pour le tableau

■ Mémoire utilisée pour 10,000 entrées est ~40Ko

public static void main(String[] args) {ArrayList myArrayList = new ArrayList();

}

Page 21: Java Code to Java Heap - En Français

21 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Autres types de “Collections”

■ StringBuffers peut être consideré comme un type de collection– “A thread-safe, mutable sequence of characters...

.... – Every string buffer has a capacity. As long as the length of the character sequence

contained in the string buffer does not exceed the capacity, it is not necessary toallocate a new internal buffer array. If the internal buffer overflows, it is automaticallymade larger.”

• Java Platform SE 6 API doc

■ L'implémentation est un tableau de char

■ Capacité par défaut: 16

■ Taille vide: 72 octets

■ Mémoire utilisée: 24 octets pour le StringBuffer

public static void main(String[] args) {StringBuffer myStringBuffer = new StringBuffer();

}

Page 22: Java Code to Java Heap - En Français

22 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Récapitulatif sur les Collections

Collection Capacité par défaut

Taille vide Utilisée pour 10,000

HashSet 16 144 360Ko

HashMap 16 128 360Ko

Hashtable 11 104 360Ko

LinkedList 1 48 240Ko

ArrayList 10 88 40Ko

StringBuffer 16 72 24

Page 23: Java Code to Java Heap - En Français

23 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Hash* collections vs les autres

■ Les Hash* collections sont beaucoup plus grandes– x9 plus grande que ArrayList

■ La taille supplémentaire permet la recherche / insertion / suppression rapide– Constante pour les collections Hash– Augmentation linéaire pour les collections Array

■ L'utilisation d'une collection coûteuse en mémoire peut être la bonne chose à faire– L'important est de savoir quelle collection remplie le mieux la tâche !

Page 24: Java Code to Java Heap - En Français

24 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

L'espace vide dans des collections

■ Les collections qui contiennent des espaces vides introduisent une charge supplémentaire■ La taille de la collection par défaut peut ne pas être correcte

java.lang.StringBuffer

char[]

0 32 64 96 128 160 192 224 256 288 320

Class Flags Locks count

Class Flags Locks charSize

value

charcharcharcharcharcharcharcharcharcharcharcharcharcharcharM Y S T R I N G

352 384 416 448 480 512 544 576 608 640

■ La valeur par défaut de 16 pour la StringBuffer n'est pas optimale pour une chaîne de 9 caractères

– 7 entrées supplémentaires dans le char[]– 112 octets inutiles

public static void main(String[] args) { StringBuffer myStringBuffer = new StringBuffer(“My String”);}

Page 25: Java Code to Java Heap - En Français

25 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Expansion des collections

■ Lorsque les collections atteignent leur taille limite, elles s'agrandissent– Augmente considérablement la taille– Réduit considérablement le ratio “mémoire allouée/mémoire utilisée” (Fill Ratio)

■ Mémoire utilisée supplémentaire:

java.lang.StringBuffer

char[]

0 32 64 96 128 160 192 224 256 288 320

Class Flags Locks count

Class Flags Locks charSize

value

charcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharcharM Y S T R I N G O F T E X T

352 384 416 448 480 512 544 576 608 640

■ 16 nouvelles entrées char [] pour seulement stocker un char supplémentaire– 240 octets inutiles

Page 26: Java Code to Java Heap - En Français

26 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Récapitulatif sur les Collections

Collection Capacité par défaut

Taille vide Utilisée pour 10,000

Aggrandissement

HashSet 16 144 360Ko x2

HashMap 16 128 360Ko x2

Hashtable 11 104 360Ko x2 + 1

LinkedList 1 48 240Ko +1

ArrayList 10 88 40Ko x1.5

StringBuffer 16 72 24 x2

Page 27: Java Code to Java Heap - En Français

27 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Récapitulatif sur les Collections

■ Les Collections existent en grand nombres dans de nombreuses applications Java

■ Exemple: IBM WebSphere Application Server avec PlantsByWebSphere– 5 utilisateurs, 206MB Java heap:

■ 16% du Tas Java pour les Collection !!

HashTable 262,234 instances, 26.5MBof Java heapWeakHashMap 19,562 instances 2.6MB of Java heapHashMap 10,600 instances 2.3MB of Java heapArrayList 9,530 instances 0.3MB of Java heapHashSet 1,551 instances 1.0MB of Java heapVector 1,271 instances 0.04MBof Java heapLinkedList 1,148 instances 0.1MB of Java heapTreeMap 299 instances 0.03MB of Java heap

306,195 32.9MB

Page 28: Java Code to Java Heap - En Français

28 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Analyse de vos Collections

■ Eclipse Memory Analyzer Tool (MAT) permet l'analyse des Collections Java:

Page 29: Java Code to Java Heap - En Français

29 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Analyse de vos Collections

■ Vous pouvez selectionner une Collection Java (par example: java.util.Hashtable)

Page 30: Java Code to Java Heap - En Français

30 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Analyse de vos Collections

■ 127,016 instances vides de java.util.Hashtable

Page 31: Java Code to Java Heap - En Français

31 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Analyse de vos Collections

■ Vous pouvez obtenir la liste des objets java.util.Hashtable

Page 32: Java Code to Java Heap - En Français

32 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Analyse de vos Collections

■ Les objets java.util.Hashtable stockent les données de session!

Page 33: Java Code to Java Heap - En Français

33 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Techniques pour réduire l'utilisation mémoire

■ Créer les Collections au bon moment– Ne créer une Collection que lorsque vous avez quelque chose à mettre dedans

■ Ne pas créer de Collections pour un Objet unique!

■ Choisir la taille des Collections correctement– Si seulement 2 entrées seront enregistrées, créer une collection de taille 2:

■ Éviter l'expansion de grandes collections en raison de l'algorithme x2– 32MB de mémoire utilisée pour 17MB de donnéés

■ Les collections ne rétrécissent pas une fois étendues– Il peut être nécessaire de récréer la Collection

HashMap myHashMap = new HashMap(2);

Page 34: Java Code to Java Heap - En Français

34 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Récapitulatif

■ Les collections surchargent considérablement vos données!

■ Les applications doivent:– Utiliser les bons types de collection,– Eviter les Collections vides

■ Eclipse Memory Analyzer Tool peut analyser vos Collections Java– et l'utilisation de la mémoire de l'ensemble de votre application

Page 35: Java Code to Java Heap - En Français

35 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Lire l'article:

From Java Code to Java Heap on IBM developerWorks:

http://www.ibm.com/developerworks/java/library/j-codetoheap/index.html

Page 36: Java Code to Java Heap - En Français

36 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Télécharger la présentation:

From Java Code to Java Heap on Slideshare.net:

http://www.slideshare.net/cnbailey/java-code-to-java-heap

Page 37: Java Code to Java Heap - En Français

37 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Références

■ Get Products and Technologies:– IBM Monitoring and Diagnostic Tools for Java:

• https://www.ibm.com/developerworks/java/jdk/tools/– Eclipse Memory Analyzer Tool:

• http://eclipse.org/mat/downloads.php

■ Learn:– Debugging from Dumps:

• http://www.ibm.com/developerworks/java/library/j-memoryanalyzer/index.html– Why the Memory Analyzer (with IBM Extensions) isn't just for memory leaks anymore:

• http://www.ibm.com/developerworks/websphere/techjournal/1103_supauth/1103_supauth.html

■ Discuss:– IBM on Troubleshooting Java Applications Blog:

• https://www.ibm.com/developerworks/mydeveloperworks/blogs/troubleshootingjava/– IBM Java Runtimes and SDKs Forum:

• http://www.ibm.com/developerworks/forums/forum.jspa?forumID=367&start=0

Page 38: Java Code to Java Heap - En Français

38 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation

Copyright and Trademarks

© IBM Corporation 2012. All Rights Reserved.

IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corp., and registered in many jurisdictions worldwide.

Other product and service names might be trademarks of IBM or other companies.

A current list of IBM trademarks is available on the Web – see the IBM “Copyright and trademark information” page at URL: www.ibm.com/legal/copytrade.shtml