SlideShare a Scribd company logo
OutOfMemoryError:
What is the cost of Java Objects?
Jean-Philippe BEMPEL @jpbempel
Performance Architect http://jpbempel.blogspot.com
© ULLINK 2016
© ULLINK 2016 2
Agenda
© ULLINK 2016 3
• java.lang.Object
• CompressedOops
• Structure sizes
• Troubleshooting
• War stories
• Solutions
© ULLINK 2016 4
java.lang.Object
java.lang.Object
© ULLINK 2016 5
Fields 32 bits 64 bits 64 bits
CompressedOops
mark word 4 bytes 8 bytes 8 bytes
klass pointer 4 bytes 8 bytes 4 bytes
Total 8 bytes 16 bytes 12 bytes (but 16 with
padding/alignment)
java.lang.Object
© ULLINK 2016 6
© ULLINK 2016 7
CompressedOops
• 32 bits pointer can address 4GB
• Memory addresses are aligned (4 or 8)
• Objects reside only on address 8 multiple
• Last 3 bits are not effectively used
• Use the 3 bits to increase addressable space
CompressedOops
© ULLINK 2016 8
Example:
Address 88 in binary:
101 1000
will be encoded as (3 bits shift)
1011
Allows to encode 8 times more object
addresses then with raw 32 bits addressing
=> Maximum addressable is then 32GB
CompressedOops saves ~20-30% memory
Activated by default on 64bits JVM
JVM option: -XX:+UseCompressedOops
CompressedOops
© ULLINK 2015 9
© ULLINK 2016 10
Padding
Padding
© ULLINK 2016 11
With 8 bytes field alignment, padding occurs
What is the real size of this class:
class Data
{
long l;
boolean b;
int i;
char c;
String str;
}
Padding
© ULLINK 2016 12
Heap Dump analysis with profilers can give a good
estimation
Real size depends on 32bits/64Bits/CompressedOops
A precise way: Java Object Layout (JOL)
java -XX:+UseCompressedOops -jar jol-internals.jar java.lang.Object
Running 64-bit HotSpot VM.
Using compressed references with 3-bit shift.
Objects are 8 bytes aligned.
Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000)
4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000)
8 4 (object header) 6d 05 88 df (0110 1101 0000 0101 1000 1000 1101 1111)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes (estimated, add this JAR via -javaagent: to get accurate result)
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
Padding
© ULLINK 2016 13
Class Data:
java -XX:+UseCompressedOops -classpath .;jol-internals.jar org.openjdk.jol.MainObjectInternals
Data
Running 64-bit HotSpot VM.
Using compressed references with 3-bit shift.
Objects are 8 bytes aligned.
Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Data object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000)
4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000)
8 4 (object header) b6 8c 91 df (1011 0110 1000 1100 1001 0001 1101 1111)
12 4 int Data.i 0
16 8 long Data.l 0
24 2 char Data.c
26 1 boolean Data.b false
27 1 (alignment/padding gap) N/A
28 4 String Data.str null
Instance size: 32 bytes (estimated, add this JAR via -javaagent: to get accurate result)
Space losses: 1 bytes internal + 0 bytes external = 1 bytes total
© ULLINK 2016 14
Structure sizes
Arrays
© ULLINK 2016 15
Arrays have additional int for size
Header for 64 bits + CompressedOops: 16 bytes
byte[32] => header + 1*32 bytes = 48 bytes
short[32] => header + 2*32 bytes = 80 bytes
char[32] => header + 2*32 bytes = 80 bytes
int[32] => header + 4*32 bytes = 144 bytes
long[32] => header + 8*32 bytes = 272 bytes
double[32] => header + 8*32 bytes = 272 bytes
Object[32] => header + RefSize*32 bytes = 144
bytes
java.lang.String
© ULLINK 2016 16
String class fields
1.6.0_45 (32 bytes)
• char[] value
• int hash
• int count
• int offset
1.7.0_55 (24 bytes)
• char[] value
• int hash
• int hash32
1.8.0_60 (24 bytes)
• char[] value
• int hash
String.subString()
● <1.7.0_06 => shared char[]
● >= 1.7.0_06 => make copy of char[]
java.lang.String
© ULLINK 2016 17
Class String + char[]
example for string foo
Class String Size + char array header + 2*3 bytes = 24 + 16 + 6 = 46
bytes
overhead = 1433%
for string of 100 characters:
Class String Size + char array header + 2*100 bytes = 24 + 16 + 200 = 240
bytes
overhead = 140%
java.util.LinkedList
© ULLINK 2016 18
Class LinkedList: 32 bytes
Class Node: 24 bytes
Example for 100 elements: 32 + 100*24 = 2432 bytes
java.util.ArrayList
© ULLINK 2016 19
Class ArrayList: 24 bytes
Object[]: 16 + n*4 bytes
Example for 100 elements: 24 + 16 + 100*4 = 440 bytes
java.util.HashMap
© ULLINK 2016 20
class HashMap: 48 bytes
Entry[]: 16 + n*4 bytes
class Entry: 32 bytes
Example for 100 key/value pairs:
48 + 16 + 256*4 + 100*32 = 4288 bytes
java.util.HashMap
© ULLINK 2016 21
● entrySet() called => add EntrySet instance (16 bytes)
● keySet called => add KeySet instance (16 bytes)
● values() called => add Values instance (16 bytes)
For those inner classes this is object header + outer ref
java.util.HashMap.Values object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 HashMap Values.this$0 N/A
Instance size: 16 bytes (estimated, the sample instance is not available)
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
java.util.HashSet
© ULLINK 2016 22
class HashSet: 16 bytes
example for 100 elements: 16 + HashMap cost = 4304 bytes
java.util.TreeMap
© ULLINK 2016 23
class TreeMap: 48 bytes
class Entry: 40 bytes (double penalty)
example for 100 elements:
48 + 100*40 = 4048 bytes
java.util.TreeMap
© ULLINK 2016 24
● entrySet() called => add EntrySet instance (16 bytes)
● keySet called => add KeySet instance (16 bytes)
● values() called => add Values instance (16 bytes)
● navigableKeySet called => add KeySet instance (16 bytes)
● descendingMap called => add DescendingSubMap instance
(56 bytes)
java.util.concurrent.ConcurrentHashMap (1.8)
© ULLINK 2016 25
class ConcurrentHashMap: 64 bytes
class Node: 32 bytes
Example for 100 elements:
64 + 16 + 256*4 + 100*32 = 4304 bytes
java.util.concurrent.ConcurrentHashMap (1.7)
© ULLINK 2016 26
class ConcurrentHashMap: 48 bytes
Segment[]: 16+16*4 = 80 bytes
class Segment: 40 bytes + class Sync: 32 bytes
HashEntry[]: 16 + n*4 bytes
class HashEntry: 32 bytes
example for 100 elements:
48 + 80 + 16*(40 + 32 + 16 + 16*4) + 32*100 = 5760 bytes
Summary
© ULLINK 2016 27
Collections Overhead for 100
ArrayList 440 bytes
LinkedList 2432 bytes
TreeMap 4048 bytes
HashMap 4288 bytes
HashSet 4304 bytes
ConcurrentHashMap (1.8) 4304 bytes
ConcurrentHashMap (1.7) 5760 bytes
© ULLINK 2016 28
Troubleshooting
Troubleshooting: Class histogram
© ULLINK 2016 29
jmap -histo <pid>
num #instances #bytes class name
----------------------------------------------
1: 73903 5978960 [C
2: 4309 4085624 [I
3: 2790 1730968 [B
4: 49641 1191384 java.lang.String
5: 22080 706560 java.util.HashMap$Node
6: 5420 571328 java.lang.Class
7: 7408 431080 [Ljava.lang.Object;
8: 2908 331584 [Ljava.util.HashMap$Node;
9: 1339 289224 sun.java2d.SunGraphics2D
10: 2882 253616 java.lang.reflect.Method
11: 6586 227248 [Ljava.lang.Class;
12: 1632 223768 [Ljava.lang.String;
13: 4973 198920 java.security.AccessControlContext
14: 3880 186240 java.util.HashMap
15: 4890 156480 java.util.Hashtable$Entry
16: 5376 129024 java.lang.StringBuilder
17: 3293 105376 java.lang.ref.WeakReference
18: 1424 102528 java.awt.geom.AffineTransform
19: 3186 101952 java.awt.Rectangle
20: 3005 96160 java.util.concurrent.ConcurrentHashMap$Node
JVM Option: -XX:+PrintClassHistogramBeforeFullGC
Includes class histogram in GC logs
Troubleshooting: Heap dump
© ULLINK 2016 30
To generate it:
● jmap -dump:live,format=b,file=heap.hprof <pid>
● JVM Options:
○ -XX:+HeapDumpOnOutOfMemoryError
○ -XX:HeapDumpPath=../logs
● JMX: com.sun.management:type=HotSpotDiagnostic.dumpHeap(filename,
liveonly)
To exploit heap dump:
● visualVM
● YourKit
● Eclipse MAT
Troubleshooting: Heap dump
© ULLINK 2015 31
Troubleshooting: Heap dump
© ULLINK 2016 32
© ULLINK 2016 33
War stories
War stories
© ULLINK 2016 34
Overhead structures example:
251614.001: [GC 251614.001: [ParNew (promotion failed): 943744K->940573K(943744K), 0.9248570 secs]251614.926:
[Class Histogram:
num #instances #bytes class name
----------------------------------------------
1: 421751083 20244051984 java.util.concurrent.ConcurrentHashMap$HashEntry
2: 23327688 12130397760 com.ullink.ulmonitoring.api.model.SimpleBMOrder
3: 23428922 6694953456 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
4: 11574497 5007170576 [C
5: 178973 2246627208 [B
6: 23357783 1681760376 java.util.concurrent.ConcurrentHashMap
7: 26115162 1253527776 java.util.HashMap$Entry
8: 23386843 1122568464 java.util.concurrent.locks.ReentrantLock$NonfairSync
9: 23381591 1122316368 java.util.concurrent.ConcurrentHashMap$Segment
10: 27363594 1094543760 java.lang.String
11: 22350856 894034240 com.ullink.ulmonitoring.api.model.IndentedHelper
12: 22350849 894033960 com.ullink.ulmonitoring.api.model.OrderLine
13: 23357784 747639528 [Ljava.util.concurrent.ConcurrentHashMap$Segment;
14: 22350792 715225344 com.ullink.ulmonitoring.extension.view.HierarchicalOrderLine
15: 23356168 560548032 com.ullink.ulmonitoring.api.model.property.PropertyHolderImpl
...
817: 40 4480 com.ullink.ulmonitoring.extension.view.HierarchicalViewDataContainer
War stories
© ULLINK 2016 35
Client class (104 bytes)
com.ullink.oms.model.Client object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000)
4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000)
8 4 (object header) 3b de 91 df (0011 1011 1101 1110 1001 0001 1101 1111)
12 4 String OdisysObject.id null
16 4 String Client.type null
20 4 Capacity Client.capacity null
24 4 String Client.description null
28 4 String Client.currency null
32 4 String Client.parentClientId null
36 4 Collection Client.contact null
40 4 String Client.data null
44 4 Contact Client.office null
48 4 String Client.originCountry null
52 4 Boolean Client.allowUnknownAccount null
56 4 String Client.connectionDetails null
60 4 String Client.backoffice null
64 4 Policy Client.policy null
68 4 ConfirmationSchedule Client.confirmationSchedule null
72 4 Collection Client.stampExemptCountries null
76 4 Map Client.customProperties null
80 4 Delivery Client.delivery null
84 4 Warehousing Client.warehousing null
88 4 Map Client.externalIds null
92 4 String Client.feeId null
96 4 Boolean Client.active null
100 4 (loss due to the next object alignment)
Instance size: 104 bytes (estimated, add this JAR via -javaagent: to get accurate result)
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
War stories
© ULLINK 2016 36
num #instances #bytes class name
----------------------------------------------
1: 116925840 18595529736 [C
2: 100508918 10452927472 com.ullink.oms.model.Client
3: 116908590 2805806160 java.lang.String
4: 39690 773638968 [B
5: 77391 596989120 [Ljava.lang.Object;
6: 6116590 195730880 java.util.HashMap$Entry
7: 2370792 149697448 [Ljava.util.HashMap$Entry;
8: 2406042 115490016 java.util.HashMap
9: 149661 20530168 <constMethodKlass>
10: 149661 19166816 <methodKlass>
Event user.update
1 user with 32 560 Clients
3796 events = 245M Client instances => 25GB + Strings
Persistence delete operation keep objects in a Collection
War stories
© ULLINK 2016 37
Instrument class:
● 62 ref fields
● String, BigDecimal, Boolean, Double, Collection,
Custom Classes (AlternateId (56B), Time (80B),
Enums (24B)...)
Size: 264 bytes
War stories
© ULLINK 2016 38
Instrument Referential: 3M
Only Instrument instances: 792MB
Add 8 chars for instrumentId: +168MB (56*3M)
Add 4 chars for symbol: + 312MB (48*3M + 56*3M)
Add settlement date: + 408MB (56*3M + 80*3M)
Add 3 chars for currency: + 138MB (46*3M)
Total 1818MB
Other representation:
for each instrument : Map<String, MDRecord>
© ULLINK 2016 39
Solutions
Solutions
© ULLINK 2016 40
• Flyweight/internalizers
• ArrayList vs LinkedList
• HashMap => OpenAddressingHashMap
Measure, don’t guess!
Kirk Pepperdine & Jack Shirazi
Measure
© ULLINK 2016 41
Measure, don’t premature!
Measure
© ULLINK 2016 42
References
© ULLINK 2016 43
Java Object Layout:
http://openjdk.java.net/projects/code-tools/jol/
What Heap Dumps Are Lying To You About:
http://shipilev.net/blog/2014/heapdump-is-a-lie/
From Java code to Java heap:
http://www.ibm.com/developerworks/library/j-codetoheap/
Building Memory-efficient Java Applications: Practices and
Challenges:
http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory-
efficient-java-tutorial.pdf
Q & A
Jean-Philippe BEMPEL @jpbempel
Performance Architect http://jpbempel.blogspot.com
© ULLINK 2016

More Related Content

PPTX
Php forum2015 tomas_final
Bertrand Matthelie
 
PDF
4java Basic Syntax
Adil Jafri
 
PPTX
BGOUG15: JSON support in MySQL 5.7
Georgi Kodinov
 
PDF
Meet scala
Wojciech Pituła
 
PDF
Indexing and Query Optimizer (Mongo Austin)
MongoDB
 
PDF
Using JSON with MariaDB and MySQL
Anders Karlsson
 
PDF
ROracle
Mohamed Magdy
 
PPTX
Basic java, java collection Framework and Date Time API
jagriti srivastava
 
Php forum2015 tomas_final
Bertrand Matthelie
 
4java Basic Syntax
Adil Jafri
 
BGOUG15: JSON support in MySQL 5.7
Georgi Kodinov
 
Meet scala
Wojciech Pituła
 
Indexing and Query Optimizer (Mongo Austin)
MongoDB
 
Using JSON with MariaDB and MySQL
Anders Karlsson
 
ROracle
Mohamed Magdy
 
Basic java, java collection Framework and Date Time API
jagriti srivastava
 

What's hot (20)

PPTX
Beyond parallelize and collect - Spark Summit East 2016
Holden Karau
 
PDF
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Sages
 
PPT
Fast querying indexing for performance (4)
MongoDB
 
ODP
Intravert Server side processing for Cassandra
Edward Capriolo
 
PDF
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 
KEY
Python在豆瓣的应用
Qiangning Hong
 
PPTX
MongoDB and Indexes - MUG Denver - 20160329
Douglas Duncan
 
PDF
Spark workshop
Wojciech Pituła
 
PDF
Clojure for Data Science
henrygarner
 
PDF
NoSQL для PostgreSQL: Jsquery — язык запросов
CodeFest
 
PPTX
Clojure for Data Science
Mike Anderson
 
ODP
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
Nikolay Samokhvalov
 
PDF
Clojure class
Aysylu Greenberg
 
PDF
Windows 10 Nt Heap Exploitation (Chinese version)
Angel Boy
 
PDF
Beyond tf idf why, what & how
lucenerevolution
 
PPTX
Webinar: Index Tuning and Evaluation
MongoDB
 
PDF
Beyond Shuffling, Tips and Tricks for Scaling Apache Spark updated for Spark ...
Holden Karau
 
PDF
Clojure: The Art of Abstraction
Alex Miller
 
PDF
D3.js workshop
Anton Katunin
 
KEY
R for Pirates. ESCCONF October 27, 2011
Mandi Walls
 
Beyond parallelize and collect - Spark Summit East 2016
Holden Karau
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Sages
 
Fast querying indexing for performance (4)
MongoDB
 
Intravert Server side processing for Cassandra
Edward Capriolo
 
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 
Python在豆瓣的应用
Qiangning Hong
 
MongoDB and Indexes - MUG Denver - 20160329
Douglas Duncan
 
Spark workshop
Wojciech Pituła
 
Clojure for Data Science
henrygarner
 
NoSQL для PostgreSQL: Jsquery — язык запросов
CodeFest
 
Clojure for Data Science
Mike Anderson
 
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
Nikolay Samokhvalov
 
Clojure class
Aysylu Greenberg
 
Windows 10 Nt Heap Exploitation (Chinese version)
Angel Boy
 
Beyond tf idf why, what & how
lucenerevolution
 
Webinar: Index Tuning and Evaluation
MongoDB
 
Beyond Shuffling, Tips and Tricks for Scaling Apache Spark updated for Spark ...
Holden Karau
 
Clojure: The Art of Abstraction
Alex Miller
 
D3.js workshop
Anton Katunin
 
R for Pirates. ESCCONF October 27, 2011
Mandi Walls
 
Ad

Similar to Out ofmemoryerror what is the cost of java objects (20)

PDF
Financial Portfolio Management with Java on Steroids - JAX Finance 2016
aixigo AG
 
PPTX
A G1GC Saga-KCJUG.pptx
Monica Beckwith
 
PDF
A compact bytecode format for JavaScriptCore
Tadeu Zagallo
 
PDF
JavaOne 2013: Memory Efficient Java
Chris Bailey
 
PDF
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
Herman Wu
 
PPTX
Why learn Internals?
Shaul Rosenzwieg
 
PDF
CIFAR-10 for DAWNBench: Wide ResNets, Mixup Augmentation and "Super Convergen...
Thom Lane
 
PDF
Forgive me for i have allocated
Tomasz Kowalczewski
 
PDF
Sperasoft‬ talks j point 2015
Sperasoft
 
PPT
JVM performance options. How it works
Dmitriy Dumanskiy
 
PDF
Spark schema for free with David Szakallas
Databricks
 
PDF
Game of Performance: A Song of JIT and GC
Monica Beckwith
 
PDF
Save Java memory
JavaDayUA
 
PPTX
Myths of Big Partitions (Robert Stupp, DataStax) | Cassandra Summit 2016
DataStax
 
PDF
Why GC is eating all my CPU?
Roman Elizarov
 
PDF
Åsted .Net (CSI .Net)
Kjetil Klaussen
 
ODP
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Carol McDonald
 
PPTX
Java/Scala Lab 2016. Сергей Моренец: Способы повышения эффективности в Java 8.
GeeksLab Odessa
 
PPTX
Java Performance Tips (So Code Camp San Diego 2014)
Kai Chan
 
PDF
Sorry - How Bieber broke Google Cloud at Spotify
Neville Li
 
Financial Portfolio Management with Java on Steroids - JAX Finance 2016
aixigo AG
 
A G1GC Saga-KCJUG.pptx
Monica Beckwith
 
A compact bytecode format for JavaScriptCore
Tadeu Zagallo
 
JavaOne 2013: Memory Efficient Java
Chris Bailey
 
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
Herman Wu
 
Why learn Internals?
Shaul Rosenzwieg
 
CIFAR-10 for DAWNBench: Wide ResNets, Mixup Augmentation and "Super Convergen...
Thom Lane
 
Forgive me for i have allocated
Tomasz Kowalczewski
 
Sperasoft‬ talks j point 2015
Sperasoft
 
JVM performance options. How it works
Dmitriy Dumanskiy
 
Spark schema for free with David Szakallas
Databricks
 
Game of Performance: A Song of JIT and GC
Monica Beckwith
 
Save Java memory
JavaDayUA
 
Myths of Big Partitions (Robert Stupp, DataStax) | Cassandra Summit 2016
DataStax
 
Why GC is eating all my CPU?
Roman Elizarov
 
Åsted .Net (CSI .Net)
Kjetil Klaussen
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Carol McDonald
 
Java/Scala Lab 2016. Сергей Моренец: Способы повышения эффективности в Java 8.
GeeksLab Odessa
 
Java Performance Tips (So Code Camp San Diego 2014)
Kai Chan
 
Sorry - How Bieber broke Google Cloud at Spotify
Neville Li
 
Ad

More from Jean-Philippe BEMPEL (18)

PDF
Mastering GC.pdf
Jean-Philippe BEMPEL
 
PDF
Javaday 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneur...
Jean-Philippe BEMPEL
 
PDF
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
Jean-Philippe BEMPEL
 
PDF
Tools in action jdk mission control and flight recorder
Jean-Philippe BEMPEL
 
PDF
Understanding JVM GC: advanced!
Jean-Philippe BEMPEL
 
PDF
Understanding low latency jvm gcs V2
Jean-Philippe BEMPEL
 
PDF
Understanding low latency jvm gcs
Jean-Philippe BEMPEL
 
PDF
Understanding jvm gc advanced
Jean-Philippe BEMPEL
 
PPTX
Clr jvm implementation differences
Jean-Philippe BEMPEL
 
PDF
Le guide de dépannage de la jvm
Jean-Philippe BEMPEL
 
PDF
OutOfMemoryError : quel est le coût des objets en java
Jean-Philippe BEMPEL
 
PDF
Low latency & mechanical sympathy issues and solutions
Jean-Philippe BEMPEL
 
PDF
Lock free programming - pro tips devoxx uk
Jean-Philippe BEMPEL
 
PDF
Lock free programming- pro tips
Jean-Philippe BEMPEL
 
PDF
Programmation lock free - les techniques des pros (2eme partie)
Jean-Philippe BEMPEL
 
PDF
Programmation lock free - les techniques des pros (1ere partie)
Jean-Philippe BEMPEL
 
PDF
Measuring directly from cpu hardware performance counters
Jean-Philippe BEMPEL
 
PDF
Devoxx france 2014 compteurs de perf
Jean-Philippe BEMPEL
 
Mastering GC.pdf
Jean-Philippe BEMPEL
 
Javaday 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneur...
Jean-Philippe BEMPEL
 
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
Jean-Philippe BEMPEL
 
Tools in action jdk mission control and flight recorder
Jean-Philippe BEMPEL
 
Understanding JVM GC: advanced!
Jean-Philippe BEMPEL
 
Understanding low latency jvm gcs V2
Jean-Philippe BEMPEL
 
Understanding low latency jvm gcs
Jean-Philippe BEMPEL
 
Understanding jvm gc advanced
Jean-Philippe BEMPEL
 
Clr jvm implementation differences
Jean-Philippe BEMPEL
 
Le guide de dépannage de la jvm
Jean-Philippe BEMPEL
 
OutOfMemoryError : quel est le coût des objets en java
Jean-Philippe BEMPEL
 
Low latency & mechanical sympathy issues and solutions
Jean-Philippe BEMPEL
 
Lock free programming - pro tips devoxx uk
Jean-Philippe BEMPEL
 
Lock free programming- pro tips
Jean-Philippe BEMPEL
 
Programmation lock free - les techniques des pros (2eme partie)
Jean-Philippe BEMPEL
 
Programmation lock free - les techniques des pros (1ere partie)
Jean-Philippe BEMPEL
 
Measuring directly from cpu hardware performance counters
Jean-Philippe BEMPEL
 
Devoxx france 2014 compteurs de perf
Jean-Philippe BEMPEL
 

Recently uploaded (20)

PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Presentation about variables and constant.pptx
safalsingh810
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 

Out ofmemoryerror what is the cost of java objects

  • 1. OutOfMemoryError: What is the cost of Java Objects? Jean-Philippe BEMPEL @jpbempel Performance Architect http://jpbempel.blogspot.com © ULLINK 2016
  • 3. Agenda © ULLINK 2016 3 • java.lang.Object • CompressedOops • Structure sizes • Troubleshooting • War stories • Solutions
  • 4. © ULLINK 2016 4 java.lang.Object
  • 5. java.lang.Object © ULLINK 2016 5 Fields 32 bits 64 bits 64 bits CompressedOops mark word 4 bytes 8 bytes 8 bytes klass pointer 4 bytes 8 bytes 4 bytes Total 8 bytes 16 bytes 12 bytes (but 16 with padding/alignment)
  • 7. © ULLINK 2016 7 CompressedOops
  • 8. • 32 bits pointer can address 4GB • Memory addresses are aligned (4 or 8) • Objects reside only on address 8 multiple • Last 3 bits are not effectively used • Use the 3 bits to increase addressable space CompressedOops © ULLINK 2016 8
  • 9. Example: Address 88 in binary: 101 1000 will be encoded as (3 bits shift) 1011 Allows to encode 8 times more object addresses then with raw 32 bits addressing => Maximum addressable is then 32GB CompressedOops saves ~20-30% memory Activated by default on 64bits JVM JVM option: -XX:+UseCompressedOops CompressedOops © ULLINK 2015 9
  • 10. © ULLINK 2016 10 Padding
  • 11. Padding © ULLINK 2016 11 With 8 bytes field alignment, padding occurs What is the real size of this class: class Data { long l; boolean b; int i; char c; String str; }
  • 12. Padding © ULLINK 2016 12 Heap Dump analysis with profilers can give a good estimation Real size depends on 32bits/64Bits/CompressedOops A precise way: Java Object Layout (JOL) java -XX:+UseCompressedOops -jar jol-internals.jar java.lang.Object Running 64-bit HotSpot VM. Using compressed references with 3-bit shift. Objects are 8 bytes aligned. Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] java.lang.Object object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000) 4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000) 8 4 (object header) 6d 05 88 df (0110 1101 0000 0101 1000 1000 1101 1111) 12 4 (loss due to the next object alignment) Instance size: 16 bytes (estimated, add this JAR via -javaagent: to get accurate result) Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
  • 13. Padding © ULLINK 2016 13 Class Data: java -XX:+UseCompressedOops -classpath .;jol-internals.jar org.openjdk.jol.MainObjectInternals Data Running 64-bit HotSpot VM. Using compressed references with 3-bit shift. Objects are 8 bytes aligned. Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes] Data object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000) 4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000) 8 4 (object header) b6 8c 91 df (1011 0110 1000 1100 1001 0001 1101 1111) 12 4 int Data.i 0 16 8 long Data.l 0 24 2 char Data.c 26 1 boolean Data.b false 27 1 (alignment/padding gap) N/A 28 4 String Data.str null Instance size: 32 bytes (estimated, add this JAR via -javaagent: to get accurate result) Space losses: 1 bytes internal + 0 bytes external = 1 bytes total
  • 14. © ULLINK 2016 14 Structure sizes
  • 15. Arrays © ULLINK 2016 15 Arrays have additional int for size Header for 64 bits + CompressedOops: 16 bytes byte[32] => header + 1*32 bytes = 48 bytes short[32] => header + 2*32 bytes = 80 bytes char[32] => header + 2*32 bytes = 80 bytes int[32] => header + 4*32 bytes = 144 bytes long[32] => header + 8*32 bytes = 272 bytes double[32] => header + 8*32 bytes = 272 bytes Object[32] => header + RefSize*32 bytes = 144 bytes
  • 16. java.lang.String © ULLINK 2016 16 String class fields 1.6.0_45 (32 bytes) • char[] value • int hash • int count • int offset 1.7.0_55 (24 bytes) • char[] value • int hash • int hash32 1.8.0_60 (24 bytes) • char[] value • int hash String.subString() ● <1.7.0_06 => shared char[] ● >= 1.7.0_06 => make copy of char[]
  • 17. java.lang.String © ULLINK 2016 17 Class String + char[] example for string foo Class String Size + char array header + 2*3 bytes = 24 + 16 + 6 = 46 bytes overhead = 1433% for string of 100 characters: Class String Size + char array header + 2*100 bytes = 24 + 16 + 200 = 240 bytes overhead = 140%
  • 18. java.util.LinkedList © ULLINK 2016 18 Class LinkedList: 32 bytes Class Node: 24 bytes Example for 100 elements: 32 + 100*24 = 2432 bytes
  • 19. java.util.ArrayList © ULLINK 2016 19 Class ArrayList: 24 bytes Object[]: 16 + n*4 bytes Example for 100 elements: 24 + 16 + 100*4 = 440 bytes
  • 20. java.util.HashMap © ULLINK 2016 20 class HashMap: 48 bytes Entry[]: 16 + n*4 bytes class Entry: 32 bytes Example for 100 key/value pairs: 48 + 16 + 256*4 + 100*32 = 4288 bytes
  • 21. java.util.HashMap © ULLINK 2016 21 ● entrySet() called => add EntrySet instance (16 bytes) ● keySet called => add KeySet instance (16 bytes) ● values() called => add Values instance (16 bytes) For those inner classes this is object header + outer ref java.util.HashMap.Values object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 HashMap Values.this$0 N/A Instance size: 16 bytes (estimated, the sample instance is not available) Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
  • 22. java.util.HashSet © ULLINK 2016 22 class HashSet: 16 bytes example for 100 elements: 16 + HashMap cost = 4304 bytes
  • 23. java.util.TreeMap © ULLINK 2016 23 class TreeMap: 48 bytes class Entry: 40 bytes (double penalty) example for 100 elements: 48 + 100*40 = 4048 bytes
  • 24. java.util.TreeMap © ULLINK 2016 24 ● entrySet() called => add EntrySet instance (16 bytes) ● keySet called => add KeySet instance (16 bytes) ● values() called => add Values instance (16 bytes) ● navigableKeySet called => add KeySet instance (16 bytes) ● descendingMap called => add DescendingSubMap instance (56 bytes)
  • 25. java.util.concurrent.ConcurrentHashMap (1.8) © ULLINK 2016 25 class ConcurrentHashMap: 64 bytes class Node: 32 bytes Example for 100 elements: 64 + 16 + 256*4 + 100*32 = 4304 bytes
  • 26. java.util.concurrent.ConcurrentHashMap (1.7) © ULLINK 2016 26 class ConcurrentHashMap: 48 bytes Segment[]: 16+16*4 = 80 bytes class Segment: 40 bytes + class Sync: 32 bytes HashEntry[]: 16 + n*4 bytes class HashEntry: 32 bytes example for 100 elements: 48 + 80 + 16*(40 + 32 + 16 + 16*4) + 32*100 = 5760 bytes
  • 27. Summary © ULLINK 2016 27 Collections Overhead for 100 ArrayList 440 bytes LinkedList 2432 bytes TreeMap 4048 bytes HashMap 4288 bytes HashSet 4304 bytes ConcurrentHashMap (1.8) 4304 bytes ConcurrentHashMap (1.7) 5760 bytes
  • 28. © ULLINK 2016 28 Troubleshooting
  • 29. Troubleshooting: Class histogram © ULLINK 2016 29 jmap -histo <pid> num #instances #bytes class name ---------------------------------------------- 1: 73903 5978960 [C 2: 4309 4085624 [I 3: 2790 1730968 [B 4: 49641 1191384 java.lang.String 5: 22080 706560 java.util.HashMap$Node 6: 5420 571328 java.lang.Class 7: 7408 431080 [Ljava.lang.Object; 8: 2908 331584 [Ljava.util.HashMap$Node; 9: 1339 289224 sun.java2d.SunGraphics2D 10: 2882 253616 java.lang.reflect.Method 11: 6586 227248 [Ljava.lang.Class; 12: 1632 223768 [Ljava.lang.String; 13: 4973 198920 java.security.AccessControlContext 14: 3880 186240 java.util.HashMap 15: 4890 156480 java.util.Hashtable$Entry 16: 5376 129024 java.lang.StringBuilder 17: 3293 105376 java.lang.ref.WeakReference 18: 1424 102528 java.awt.geom.AffineTransform 19: 3186 101952 java.awt.Rectangle 20: 3005 96160 java.util.concurrent.ConcurrentHashMap$Node JVM Option: -XX:+PrintClassHistogramBeforeFullGC Includes class histogram in GC logs
  • 30. Troubleshooting: Heap dump © ULLINK 2016 30 To generate it: ● jmap -dump:live,format=b,file=heap.hprof <pid> ● JVM Options: ○ -XX:+HeapDumpOnOutOfMemoryError ○ -XX:HeapDumpPath=../logs ● JMX: com.sun.management:type=HotSpotDiagnostic.dumpHeap(filename, liveonly) To exploit heap dump: ● visualVM ● YourKit ● Eclipse MAT
  • 33. © ULLINK 2016 33 War stories
  • 34. War stories © ULLINK 2016 34 Overhead structures example: 251614.001: [GC 251614.001: [ParNew (promotion failed): 943744K->940573K(943744K), 0.9248570 secs]251614.926: [Class Histogram: num #instances #bytes class name ---------------------------------------------- 1: 421751083 20244051984 java.util.concurrent.ConcurrentHashMap$HashEntry 2: 23327688 12130397760 com.ullink.ulmonitoring.api.model.SimpleBMOrder 3: 23428922 6694953456 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry; 4: 11574497 5007170576 [C 5: 178973 2246627208 [B 6: 23357783 1681760376 java.util.concurrent.ConcurrentHashMap 7: 26115162 1253527776 java.util.HashMap$Entry 8: 23386843 1122568464 java.util.concurrent.locks.ReentrantLock$NonfairSync 9: 23381591 1122316368 java.util.concurrent.ConcurrentHashMap$Segment 10: 27363594 1094543760 java.lang.String 11: 22350856 894034240 com.ullink.ulmonitoring.api.model.IndentedHelper 12: 22350849 894033960 com.ullink.ulmonitoring.api.model.OrderLine 13: 23357784 747639528 [Ljava.util.concurrent.ConcurrentHashMap$Segment; 14: 22350792 715225344 com.ullink.ulmonitoring.extension.view.HierarchicalOrderLine 15: 23356168 560548032 com.ullink.ulmonitoring.api.model.property.PropertyHolderImpl ... 817: 40 4480 com.ullink.ulmonitoring.extension.view.HierarchicalViewDataContainer
  • 35. War stories © ULLINK 2016 35 Client class (104 bytes) com.ullink.oms.model.Client object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (0000 0001 0000 0000 0000 0000 0000 0000) 4 4 (object header) 00 00 00 00 (0000 0000 0000 0000 0000 0000 0000 0000) 8 4 (object header) 3b de 91 df (0011 1011 1101 1110 1001 0001 1101 1111) 12 4 String OdisysObject.id null 16 4 String Client.type null 20 4 Capacity Client.capacity null 24 4 String Client.description null 28 4 String Client.currency null 32 4 String Client.parentClientId null 36 4 Collection Client.contact null 40 4 String Client.data null 44 4 Contact Client.office null 48 4 String Client.originCountry null 52 4 Boolean Client.allowUnknownAccount null 56 4 String Client.connectionDetails null 60 4 String Client.backoffice null 64 4 Policy Client.policy null 68 4 ConfirmationSchedule Client.confirmationSchedule null 72 4 Collection Client.stampExemptCountries null 76 4 Map Client.customProperties null 80 4 Delivery Client.delivery null 84 4 Warehousing Client.warehousing null 88 4 Map Client.externalIds null 92 4 String Client.feeId null 96 4 Boolean Client.active null 100 4 (loss due to the next object alignment) Instance size: 104 bytes (estimated, add this JAR via -javaagent: to get accurate result) Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
  • 36. War stories © ULLINK 2016 36 num #instances #bytes class name ---------------------------------------------- 1: 116925840 18595529736 [C 2: 100508918 10452927472 com.ullink.oms.model.Client 3: 116908590 2805806160 java.lang.String 4: 39690 773638968 [B 5: 77391 596989120 [Ljava.lang.Object; 6: 6116590 195730880 java.util.HashMap$Entry 7: 2370792 149697448 [Ljava.util.HashMap$Entry; 8: 2406042 115490016 java.util.HashMap 9: 149661 20530168 <constMethodKlass> 10: 149661 19166816 <methodKlass> Event user.update 1 user with 32 560 Clients 3796 events = 245M Client instances => 25GB + Strings Persistence delete operation keep objects in a Collection
  • 37. War stories © ULLINK 2016 37 Instrument class: ● 62 ref fields ● String, BigDecimal, Boolean, Double, Collection, Custom Classes (AlternateId (56B), Time (80B), Enums (24B)...) Size: 264 bytes
  • 38. War stories © ULLINK 2016 38 Instrument Referential: 3M Only Instrument instances: 792MB Add 8 chars for instrumentId: +168MB (56*3M) Add 4 chars for symbol: + 312MB (48*3M + 56*3M) Add settlement date: + 408MB (56*3M + 80*3M) Add 3 chars for currency: + 138MB (46*3M) Total 1818MB Other representation: for each instrument : Map<String, MDRecord>
  • 39. © ULLINK 2016 39 Solutions
  • 40. Solutions © ULLINK 2016 40 • Flyweight/internalizers • ArrayList vs LinkedList • HashMap => OpenAddressingHashMap
  • 41. Measure, don’t guess! Kirk Pepperdine & Jack Shirazi Measure © ULLINK 2016 41
  • 43. References © ULLINK 2016 43 Java Object Layout: http://openjdk.java.net/projects/code-tools/jol/ What Heap Dumps Are Lying To You About: http://shipilev.net/blog/2014/heapdump-is-a-lie/ From Java code to Java heap: http://www.ibm.com/developerworks/library/j-codetoheap/ Building Memory-efficient Java Applications: Practices and Challenges: http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory- efficient-java-tutorial.pdf
  • 44. Q & A Jean-Philippe BEMPEL @jpbempel Performance Architect http://jpbempel.blogspot.com © ULLINK 2016