More Related Content
Similar to Java Code to Java Heap - En Français (20)
More from Chris Bailey (20)
Java Code to Java Heap - En Français
- 1. Chris Bailey – IBM Java Service Architect
9th September 2012
From Java Code to Java Heap
Comprendre l'utilisation mémoire de votre application
© 2012 IBM Corporation
- 2. 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
2 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 3. Agenda
■ Introduction à la gestion de la mémoire
■ Anatomie d'un objet Java
■ Comprendre les Collections Java
■ Analyser votre Collection Java
3 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 4. Introduction à la gestion de la mémoire
■ Java fonctionne comme un Systeme d'Exploitation (SE):
0 GB 2 GB 4 GB
-Xmx
SE et C-Runtime JVM Tas Natif Tas Java
0x40000000 0xC0000000
0x0 0x80000000 0xFFFFFFFF
■ L'architecture et/ou un SE 32bits offre 4 Go d'espace d'adressage par processus
– Beaucoup plus grand pour 64bits
■ 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”
■ Une partie du tas “natif” est pour la Machine Virtuelle Java (JVM)
4 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 5. 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
5 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 6. 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 !!
6 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 7. 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)
Taille/Bits
0 32 64 96 128 160 192 224 256 288 320
Pointeur Classe Flags Locks int, eg. 10 Java Objet
Pointeur Classe Flags Locks Size int, eg. 10 Array Objet
7 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 8. 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)
Taille/Bits
0 32 64 96 128 160 192 224 256 288 320
Class pointer de Classe
Pointeur Flags Locks Flags int Locks int, eg. 10 Java Objet
Class pointerde Classe
Pointeur Flags Locks Flags Size int Locks Size int, eg. 10 Array Objet
■ L'objet Integer occupe 9 fois plus de mémoire que le int !
8 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 9. Tailles des différents types
Field Type Field size/bits
32bits 64bits
Objet Tableau Objet Tableau
boolean 32 8 32 8
byte 32 8 32 8
char 32 16 32 16
short 32 16 32 16
int 32 32 32 32
float 32 32 32 32
long 64 64 64 64
double 64 64 64 64
Objets 32 32 64* 64
*32bits if Compressed References / Compressed Oops enabled
9 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 10. 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
10 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 11. Pour les objets plus complexes
■ Exemple: java.lang.String pour “MyString”:
public static void main(String[] args) {
String myString = new String("MyString");
}
Taille/Bits
0 32 64 96 128 160 192 224 256 288 320
Class pointer Flags Locks hash count offset value java.lang.String
char char char char char char char char
Class pointer Flags Locks Size
M Y S T R I N G
char[]
■ 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 !
11 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 12. Collections Java
■ Chaque Collection Java offre un niveau de fonctionnalités différent, cela induit une
consommation mémoire différente
Plus de function java.util.HashSet
Plus de mémoire
java.util.HashMap
java.util.Hashtable
java.util.LinkedList
java.util.ArrayList
■ Utiliser le mauvais type de Collection peut engranger une importante surcharge mémoire.
12 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 13. HashSet
public static void main(String[] args) {
HashSet myHashSet = new 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
13 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 14. HashMap
public static void main(String[] args) {
HashMap myHashMap = new 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
14 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 15. 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
15 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 16. Hashtable
public static void main(String[] args) {
Hashtable myHashtable = new 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
16 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 17. 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
17 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 18. LinkedList
public static void main(String[] args) {
LinkedList myLinkedList = new 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
18 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 19. 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
19 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 20. ArrayList
public static void main(String[] args) {
ArrayList myArrayList = new 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
20 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 21. Autres types de “Collections”
public static void main(String[] args) {
StringBuffer myStringBuffer = new StringBuffer();
}
■ 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 to
allocate a new internal buffer array. If the internal buffer overflows, it is automatically
made 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
21 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 22. Récapitulatif sur les Collections
Collection Capacité par Taille vide Utilisée pour
défaut 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
22 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 23. 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 !
23 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 24. 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
public static void main(String[] args) {
StringBuffer myStringBuffer = new StringBuffer(“My String”);
}
0 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512 544 576 608 640
Class Flags Locks count value
java.lang.StringBuffer
Class Flags Locks M Y
Size charcharcharcharcharcharcharchar charcharcharcharcharcharcharchar
S T R I N G char[]
■ 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
24 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 25. 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:
0 32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512 544 576 608 640
Class Flags Locks count value
java.lang.StringBuffer
Class Flags Locks M Y
Size char charcharcharcharcharcharchar charcharcharcharcharcharcharcharcharcharcharcharcharcharchar charcharcharcharcharcharcharchar
S T R I N G O F T E X T char[]
■ 16 nouvelles entrées char [] pour seulement stocker un char supplémentaire
– 240 octets inutiles
25 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 26. Récapitulatif sur les Collections
Collection Capacité par Taille vide Utilisée pour Aggrandissement
défaut 10,000
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
26 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 27. 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:
HashTable 262,234 instances, 26.5MB of Java heap
WeakHashMap 19,562 instances 2.6MB of Java heap
HashMap 10,600 instances 2.3MB of Java heap
ArrayList 9,530 instances 0.3MB of Java heap
HashSet 1,551 instances 1.0MB of Java heap
Vector 1,271 instances 0.04MBof Java heap
LinkedList 1,148 instances 0.1MB of Java heap
TreeMap 299 instances 0.03MB of Java heap
306,195 32.9MB
■ 16% du Tas Java pour les Collection !!
27 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 28. Analyse de vos Collections
■ Eclipse Memory Analyzer Tool (MAT) permet l'analyse des Collections Java:
28 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 29. Analyse de vos Collections
■ Vous pouvez selectionner une Collection Java (par example: java.util.Hashtable)
29 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 30. Analyse de vos Collections
■ 127,016 instances vides de java.util.Hashtable
30 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 31. Analyse de vos Collections
■ Vous pouvez obtenir la liste des objets java.util.Hashtable
31 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 32. Analyse de vos Collections
■ Les objets java.util.Hashtable stockent les données de session!
32 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 33. 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:
HashMap myHashMap = new HashMap(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
33 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 34. 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
34 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 35. Lire l'article:
From Java Code to Java Heap on IBM developerWorks:
http://www.ibm.com/developerworks/java/library/j-codetoheap/index.html
35 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 36. 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
36 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 37. 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_sup
auth.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
37 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation
- 38. 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
38 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2012 IBM Corporation