SlideShare une entreprise Scribd logo
1  sur  55
Télécharger pour lire hors ligne
SizeOf in Java
because size does matter!
Ludovic Orban — Software engineer, Terracotta
JavaZone 2012
Take aways
Better understanding of heap utilization &
VM memory management
Make better use of that memory
Look at some hotspot’s source code!
!
!
!
2
JavaZone 2012
Me, myself and I …
Software Engineer @ Terracotta
Ehcache, Quartz, Terracotta core & toolkit
Author of the Bitronix Transaction Manager
!
–
!
3
JavaZone 2012
Basic heap intro
Object (and other) overheads (on different platforms)
How to measure objects
Reflection
sun.misc.Unsafe
JVMTI
Attaching to a VM at runtime and loading an agent
Some platform gotchas
What this means for caching
Q&A
–
–
–
4
Agenda
Heap?
JavaZone 2012
Java needs memory
6
Class definitions (and classloaders)
Threads (and their stack)
NIO calls
JNI calls
JIT (compilation & executable code)
!
!
!
!
!
JavaZone 2012
Java needs memory for you
Needs to store your object state
myCousin = new Relative(name, Gender.MALE, 23);
All that state (or almost) goes on heap
Where in heap depends on your VM implementation
... but we don’t really care about this here and now
!
–
!
–
–
7
JavaZone 2012
Java Heap
Controlled with -Xms -Xmx
Lets you allocate a lot of memory
~4GB on 32bit system (theoretically)
16PB on 64bit system (I guess that’s still theoretical too)
!
!
–
–
8
JavaZone 2012
All managed memory
No free... only new
GC will free all unused objects for you
… unreachable object would me more accurate!
So let us use “new” and create objects …
!
!
–
!
9
Object?
(this is all Java 1.5 onwards!)
JavaZone 2012
Object memory consumption
11
Every field consumes memory …
… depending on its type
!
–
Type Bit representation Size in byte
boolean, byte 1 or 8 1
char, short 16 2
int, float 32 4
long, double 64 8
JavaZone 2012
Thanks for attending !
So that’s easy...
a class with 32 primitive byte fields would consume
32 bytes!
Let’s verify this first though (before leaving) …
!
–
!
12
JavaZone 2012
We should be almost done …
Start a VM
Load the class
(simple class, declaring 32 fields of type byte)
Measure the Memory used
Create an instance of that Class
(and keep a reference to it)
Measure the memory used (again)
Diff the two
!
!
!
!
!
!
13
The conclusion demo!
… alright not then!
JavaZone 2012
So… what’s wrong ?
16
Firstly, don’t call System.gc() ;-)
Secondly …
we’re missing 8 bytes
Maybe heap consumption isn’t that straight forward!
!
!
–
!
JavaZone 2012
Object header
Every object state on heap, also includes an object
header
Two words big
mark word : bitfields for stuff
(including synchronization state)
klass pointer : pointer to another object (a meta-object)
!
–
–
–
17
JavaZone 2012
2 words in size?
These are our missing 8 bytes!
But wait... are these always twice 4 bytes?
What about 64 bit systems?
Are these arch words?
These should, let’s try …
!
!
!
–
–
18
Second conclusion demo
… this better works this time!
JavaZone 2012
Almost there
Looks like removing a byte doesn’t buy us one byte
… even more, adding one byte
consumes more than expected
… more so, on 64 bit arch,
we’re really not getting what we expected!
… there seems to be yet more to it.
!
–
–
–
20
JavaZone 2012
Memory alignment
Aligning addresses in memory
So a new address only starts at every X bytes
CPU limitations
Cache line faulting / invalidation
The VM will (generally) align on 8 bytes
JRockit does sometimes 16 bytes
!
–
–
–
!
–
21
JavaZone 2012
… what about 64 bit arch?
compressed oops
64 bit address space?
Are you really using this?
If feasible, the VM will represent
managed pointers as 32-bit values
Avoiding wasting too much memory when not required
Still allowing applications to address up to four billion
objects (not bytes!), or a heap size of up to about 32Gb
!
!
–
–
–
–
22
JavaZone 2012
Right … pointers!
References to other objects are effectively pointers
A pointer will differ in size
Depending on the arch
Whether Compressed OOPs are used or not
!
!
–
–
23
Don’t guess, measure!
Reflection based measurements
sizeOf v1
JavaZone 2012
The truth about class layouting
26
VM will layout the instance’s state in memory
Not necessarily the way you’ve declared fields in the
class
Avoiding wasting memory
!
!
–
JavaZone 2012
Here’s what going on…
That information is kept by the VM
Referenced by the klass pointer
The VM first lays out
64 bit, 32 bit, 16 bit, 8 bit primitives and finally OOP
… but also backpads
!
–
!
–
–
27
Class layouting
Unsafe based measurements
sizeOf v2
JavaZone 2012
sun.misc.Unsafe.theUnsafe
30
Sun Oracle VM specific
Lets you do bunch of … unsafe stuff
!
!
JavaZone 2012
java.util.concurrent.Atomic*
how AtomicInteger uses Unsafe internally :!
31
10: public final boolean compareAndSet(int expect, int update) {
11: return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
12: }
static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}
JavaZone 2012
UnsafeSizeOf
Looking at the “last” field’s offset
Math is easily done
Still have to take care about the previous concerns
Object Alignment
!
–
–
•
32
JVMTI based measurements
sizeOf v3
JavaZone 2012
JVMTI
34
java.lang.instrument.Instrumentation
has getObjectSize(Object)
calls into the VM to figure the memory footprint of an
instance
and …
… returns it in bytes …
… that’s it!
!
–
!
–
–
–
JavaZone 2012
Now that sounds like … cool !
All we need is an instance of
java.lang.instrument.Instrumentation
… which is an interface
… implemented by sun.instrument.InstrumentationImpl
… which has a private constructor
The way to get such an instance is …
A Java Agent
!
–
–
–
!
–
35
JavaZone 2012
Java Agent
All we need is package it properly
… with an MANIFEST.MF file in the jar
declaring an Premain-Class
The class needs a
premain method
public static void premain(String agentArgs, Instrumentation inst);
!
–
–
!
–
•
36
Java Agent?
… as in -javaagent: ?!
You’re kidding me, right?
JavaZone 2012
agentmain
invoked by the VM when the agent is loaded …
… the agent is loaded by providing a
-javaagent:<locationToAgentJar>
argument at VM startup
Modifying VM startup scripts / arguments
is painful in … real life
We couldn’t settle for that!
!
–
!
!
38
JavaZone 2012
Introducing the AttachAPI
Introduced in JDK6
Let’s you attach to a VM process, while it is running
Including yourself
… and load an agent (jar)
Additional agentmain method required
Additional MANIFEST.MF’s Agent-Class
!
!
–
!
–
–
39
JavaZone 2012
… now, where is it ?
The AttachAPI is part of the tools.jar
Which is on the classpath …
… on OS X
… but not on Windows, nor Linux
But it’s there …
… as part of the install
!
!
–
–
!
–
40
JavaZone 2012
Loading the AttachAPI
Since it is not on the classpath
… but present
We load it ourselves
… search for it “around” JAVA_HOME
… if we find it load it !
And finally attach, load the agent… and get the
Instrumentation instance!
!
–
!
–
–
!
41
Victory !
… and should that fail, we fall back to Unsafe, …
… and if not present, fallback to Reflection based one
Gotcha!
You didn’t expect it to be that simple, did you?
JavaZone 2012
One last thing !
There is one gotcha …
Remember the GC we talked about?
It needs to store some information too …
… sometimes that information has a minimal footprint
… like with CMS!
!
!
–
–
–
44
JavaZone 2012
HotSpot CMS’ FreeChunk
1: class FreeChunk VALUE_OBJ_CLASS_SPEC {
2: friend class VMStructs;
3: // For 64 bit compressed oops, the markOop encodes both the size and the
4: // indication that this is a FreeChunk and not an object.
5: volatile size_t _size;
6: FreeChunk* _prev;
7: FreeChunk* _next;
(...)
}
45
Now we’re there …
… but why is that cool again?
JavaZone 2012
Why should I still care?
The VM manages a lot of burden for us …
But we still need to manage our heap size
… heap size that can grow pretty large!
But what should we do with all this memory?
!
!
–
!
47
JavaZone 2012
Caching
Make your app faster!
Keep data close to the processing unit …
… no, that’s not the DB, but in memory!
Make use of the large heap sizes VM offer today
Letting the cache manage the resources
!
–
–
!
!
48
JavaZone 2012
Ehcache ARC
Automatic Resource Control
Lets you size cache sizes
in bytes, …
… rather than in elements count
(when I say byte, I mean byte, kb, mb, gb, tb)
Not only caches but even all your caches
!
!
–
–
–
!
49
JavaZone 2012
Adding to the cache
cache.put( new Element(key, value) )
Will size the
the value (Object graph)
the key (Object graph)
the container(s)
!
!
–
–
–
50
JavaZone 2012
How much would you like?
Ehcache has
CacheManagers who manages …
Caches
Define your Cache or
whole CacheManager sizes in
maxBytesLocalHeap
!
–
–
!
–
51
JavaZone 2012
ehcache.xml
<ehcache maxBytesLocalHeap="512m">
<cache name="CacheOne" />
<cache name="CacheTwo" />
<cache name="CacheThree" maxBytesLocalHeap="256m" />
</ehcache>
52
It’s that easy …
… thanks to all the rest we’ve covered!
JavaZone 2012
Conclusion
… that’s not all, there is more to all that.
But it’s open source so go and see
There might still be bugs!
Bunch of VM options & optimizations to account for
We might have missed some
So, please, go download and try it out!
!
–
!
–
–
–
54
Questions?
Using SizeOf with ehcache, go visit www.ehcache.org

Contenu connexe

Tendances

It pro dev_birbilis_20101127_en
It pro dev_birbilis_20101127_enIt pro dev_birbilis_20101127_en
It pro dev_birbilis_20101127_enGeorge Birbilis
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New EvolutionAllan Huang
 
Java Tutorial to Learn Java Programming
Java Tutorial to Learn Java ProgrammingJava Tutorial to Learn Java Programming
Java Tutorial to Learn Java Programmingbusiness Corporate
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeOmar Bashir
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMKris Mok
 
JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projectsjazzman1980
 
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
212 kuliah 01   pengenalan pemrograman berorientasi objek (java)212 kuliah 01   pengenalan pemrograman berorientasi objek (java)
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)yuan99
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJAX London
 
Java compilation
Java compilationJava compilation
Java compilationMike Kucera
 
Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose
Java 9 Modules: The Duke Yet Lives That OSGi Shall DeposeJava 9 Modules: The Duke Yet Lives That OSGi Shall Depose
Java 9 Modules: The Duke Yet Lives That OSGi Shall DeposeNikita Lipsky
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Anton Arhipov
 
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Edureka!
 
Using Java from Ruby with JRuby IRB
Using Java from Ruby with JRuby IRBUsing Java from Ruby with JRuby IRB
Using Java from Ruby with JRuby IRBHiro Asari
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateAnton Keks
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia Vladimir Ivanov
 

Tendances (20)

The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
It pro dev_birbilis_20101127_en
It pro dev_birbilis_20101127_enIt pro dev_birbilis_20101127_en
It pro dev_birbilis_20101127_en
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Java New Evolution
Java New EvolutionJava New Evolution
Java New Evolution
 
Java Tutorial to Learn Java Programming
Java Tutorial to Learn Java ProgrammingJava Tutorial to Learn Java Programming
Java Tutorial to Learn Java Programming
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Jvm internals 2015 - CorkJUG
Jvm internals 2015 - CorkJUGJvm internals 2015 - CorkJUG
Jvm internals 2015 - CorkJUG
 
JRuby in Java Projects
JRuby in Java ProjectsJRuby in Java Projects
JRuby in Java Projects
 
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
212 kuliah 01   pengenalan pemrograman berorientasi objek (java)212 kuliah 01   pengenalan pemrograman berorientasi objek (java)
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
 
What's new in Java 11
What's new in Java 11What's new in Java 11
What's new in Java 11
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
 
Java compilation
Java compilationJava compilation
Java compilation
 
Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose
Java 9 Modules: The Duke Yet Lives That OSGi Shall DeposeJava 9 Modules: The Duke Yet Lives That OSGi Shall Depose
Java 9 Modules: The Duke Yet Lives That OSGi Shall Depose
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
 
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For B...
 
Using Java from Ruby with JRuby IRB
Using Java from Ruby with JRuby IRBUsing Java from Ruby with JRuby IRB
Using Java from Ruby with JRuby IRB
 
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, HibernateJava Course 15: Ant, Scripting, Spring, Hibernate
Java Course 15: Ant, Scripting, Spring, Hibernate
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
 

Similaire à Size of in java

Java virtual machine
Java virtual machineJava virtual machine
Java virtual machineNikhil Sharma
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applicationspkoza
 
A begineers guide of JAVA - Getting Started
 A begineers guide of JAVA - Getting Started A begineers guide of JAVA - Getting Started
A begineers guide of JAVA - Getting StartedRakesh Madugula
 
Mobile Application Development- Configuration and Android Installation
Mobile Application Development- Configuration and Android InstallationMobile Application Development- Configuration and Android Installation
Mobile Application Development- Configuration and Android InstallationChandrakantDivate1
 
CONFidence 2015: when something overflowing... - Peter Hlavaty
CONFidence 2015: when something overflowing... - Peter HlavatyCONFidence 2015: when something overflowing... - Peter Hlavaty
CONFidence 2015: when something overflowing... - Peter HlavatyPROIDEA
 
When is something overflowing
When is something overflowingWhen is something overflowing
When is something overflowingPeter Hlavaty
 
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDocker, Inc.
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsPhillip Koza
 
What are the different java interview questions you need to know?
What are the different java interview questions you need to know?What are the different java interview questions you need to know?
What are the different java interview questions you need to know?kanchanmahajan23
 
What are the different java interview questions you need to know?
What are the different java interview questions you need to know?What are the different java interview questions you need to know?
What are the different java interview questions you need to know?kanchanmahajan23
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecturesubnesh
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Mr. Akaash
 
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvmScala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvmSkills Matter
 
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingJakub Marchwicki
 
A Brief study on JVM A Brief study on JVM
A Brief study on JVM A Brief study on JVMA Brief study on JVM A Brief study on JVM
A Brief study on JVM A Brief study on JVMBRNSSPublicationHubI
 
MacRuby for Fun and Profit
MacRuby for Fun and ProfitMacRuby for Fun and Profit
MacRuby for Fun and ProfitJoshua Ballanco
 

Similaire à Size of in java (20)

Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Basic Java I
Basic Java IBasic Java I
Basic Java I
 
A begineers guide of JAVA - Getting Started
 A begineers guide of JAVA - Getting Started A begineers guide of JAVA - Getting Started
A begineers guide of JAVA - Getting Started
 
Mobile Application Development- Configuration and Android Installation
Mobile Application Development- Configuration and Android InstallationMobile Application Development- Configuration and Android Installation
Mobile Application Development- Configuration and Android Installation
 
Java lab lecture 1
Java  lab  lecture 1Java  lab  lecture 1
Java lab lecture 1
 
CONFidence 2015: when something overflowing... - Peter Hlavaty
CONFidence 2015: when something overflowing... - Peter HlavatyCONFidence 2015: when something overflowing... - Peter Hlavaty
CONFidence 2015: when something overflowing... - Peter Hlavaty
 
When is something overflowing
When is something overflowingWhen is something overflowing
When is something overflowing
 
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
 
Java unit 1
Java unit 1Java unit 1
Java unit 1
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Jvm internal detail
Jvm internal detailJvm internal detail
Jvm internal detail
 
What are the different java interview questions you need to know?
What are the different java interview questions you need to know?What are the different java interview questions you need to know?
What are the different java interview questions you need to know?
 
What are the different java interview questions you need to know?
What are the different java interview questions you need to know?What are the different java interview questions you need to know?
What are the different java interview questions you need to know?
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecture
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
 
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvmScala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
 
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
 
A Brief study on JVM A Brief study on JVM
A Brief study on JVM A Brief study on JVMA Brief study on JVM A Brief study on JVM
A Brief study on JVM A Brief study on JVM
 
MacRuby for Fun and Profit
MacRuby for Fun and ProfitMacRuby for Fun and Profit
MacRuby for Fun and Profit
 

Dernier

GBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of CarbohydratesGBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of CarbohydratesAreesha Ahmad
 
Microbial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxMicrobial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxCherry
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxGOWTHAMIM22
 
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
SCHISTOSOMA HEAMATOBIUM life cycle  .pdfSCHISTOSOMA HEAMATOBIUM life cycle  .pdf
SCHISTOSOMA HEAMATOBIUM life cycle .pdfDebdattaGhosh6
 
TEST BANK for Organic Chemistry 6th Edition.pdf
TEST BANK for Organic Chemistry 6th Edition.pdfTEST BANK for Organic Chemistry 6th Edition.pdf
TEST BANK for Organic Chemistry 6th Edition.pdfmarcuskenyatta275
 
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Sahil Suleman
 
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...frank0071
 
GBSN - Microbiology Lab (Compound Microscope)
GBSN - Microbiology Lab (Compound Microscope)GBSN - Microbiology Lab (Compound Microscope)
GBSN - Microbiology Lab (Compound Microscope)Areesha Ahmad
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Sérgio Sacani
 
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanPlasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanmuralinath2
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversitySteffi Friedrichs
 
mixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategymixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategyMansiBishnoi1
 
Lec 1.b Totipotency and birth of tissue culture.ppt
Lec 1.b Totipotency and birth of tissue culture.pptLec 1.b Totipotency and birth of tissue culture.ppt
Lec 1.b Totipotency and birth of tissue culture.pptasifali1111
 
Detectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureDetectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureSérgio Sacani
 
Hemoglobin metabolism: C Kalyan & E. Muralinath
Hemoglobin metabolism: C Kalyan & E. MuralinathHemoglobin metabolism: C Kalyan & E. Muralinath
Hemoglobin metabolism: C Kalyan & E. Muralinathmuralinath2
 
GBSN - Microbiology Lab (Microbiology Lab Safety Procedures)
GBSN -  Microbiology Lab (Microbiology Lab Safety Procedures)GBSN -  Microbiology Lab (Microbiology Lab Safety Procedures)
GBSN - Microbiology Lab (Microbiology Lab Safety Procedures)Areesha Ahmad
 
The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surfaceSérgio Sacani
 
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Sérgio Sacani
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsSérgio Sacani
 
A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthSérgio Sacani
 

Dernier (20)

GBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of CarbohydratesGBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
 
Microbial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxMicrobial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptx
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptx
 
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
SCHISTOSOMA HEAMATOBIUM life cycle  .pdfSCHISTOSOMA HEAMATOBIUM life cycle  .pdf
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
 
TEST BANK for Organic Chemistry 6th Edition.pdf
TEST BANK for Organic Chemistry 6th Edition.pdfTEST BANK for Organic Chemistry 6th Edition.pdf
TEST BANK for Organic Chemistry 6th Edition.pdf
 
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
 
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
 
GBSN - Microbiology Lab (Compound Microscope)
GBSN - Microbiology Lab (Compound Microscope)GBSN - Microbiology Lab (Compound Microscope)
GBSN - Microbiology Lab (Compound Microscope)
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...
 
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanPlasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere University
 
mixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategymixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategy
 
Lec 1.b Totipotency and birth of tissue culture.ppt
Lec 1.b Totipotency and birth of tissue culture.pptLec 1.b Totipotency and birth of tissue culture.ppt
Lec 1.b Totipotency and birth of tissue culture.ppt
 
Detectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureDetectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a Technosignature
 
Hemoglobin metabolism: C Kalyan & E. Muralinath
Hemoglobin metabolism: C Kalyan & E. MuralinathHemoglobin metabolism: C Kalyan & E. Muralinath
Hemoglobin metabolism: C Kalyan & E. Muralinath
 
GBSN - Microbiology Lab (Microbiology Lab Safety Procedures)
GBSN -  Microbiology Lab (Microbiology Lab Safety Procedures)GBSN -  Microbiology Lab (Microbiology Lab Safety Procedures)
GBSN - Microbiology Lab (Microbiology Lab Safety Procedures)
 
The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surface
 
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discs
 
A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on Earth
 

Size of in java

  • 1. SizeOf in Java because size does matter! Ludovic Orban — Software engineer, Terracotta
  • 2. JavaZone 2012 Take aways Better understanding of heap utilization & VM memory management Make better use of that memory Look at some hotspot’s source code! ! ! ! 2
  • 3. JavaZone 2012 Me, myself and I … Software Engineer @ Terracotta Ehcache, Quartz, Terracotta core & toolkit Author of the Bitronix Transaction Manager ! – ! 3
  • 4. JavaZone 2012 Basic heap intro Object (and other) overheads (on different platforms) How to measure objects Reflection sun.misc.Unsafe JVMTI Attaching to a VM at runtime and loading an agent Some platform gotchas What this means for caching Q&A – – – 4 Agenda
  • 6. JavaZone 2012 Java needs memory 6 Class definitions (and classloaders) Threads (and their stack) NIO calls JNI calls JIT (compilation & executable code) ! ! ! ! !
  • 7. JavaZone 2012 Java needs memory for you Needs to store your object state myCousin = new Relative(name, Gender.MALE, 23); All that state (or almost) goes on heap Where in heap depends on your VM implementation ... but we don’t really care about this here and now ! – ! – – 7
  • 8. JavaZone 2012 Java Heap Controlled with -Xms -Xmx Lets you allocate a lot of memory ~4GB on 32bit system (theoretically) 16PB on 64bit system (I guess that’s still theoretical too) ! ! – – 8
  • 9. JavaZone 2012 All managed memory No free... only new GC will free all unused objects for you … unreachable object would me more accurate! So let us use “new” and create objects … ! ! – ! 9
  • 10. Object? (this is all Java 1.5 onwards!)
  • 11. JavaZone 2012 Object memory consumption 11 Every field consumes memory … … depending on its type ! – Type Bit representation Size in byte boolean, byte 1 or 8 1 char, short 16 2 int, float 32 4 long, double 64 8
  • 12. JavaZone 2012 Thanks for attending ! So that’s easy... a class with 32 primitive byte fields would consume 32 bytes! Let’s verify this first though (before leaving) … ! – ! 12
  • 13. JavaZone 2012 We should be almost done … Start a VM Load the class (simple class, declaring 32 fields of type byte) Measure the Memory used Create an instance of that Class (and keep a reference to it) Measure the memory used (again) Diff the two ! ! ! ! ! ! 13
  • 16. JavaZone 2012 So… what’s wrong ? 16 Firstly, don’t call System.gc() ;-) Secondly … we’re missing 8 bytes Maybe heap consumption isn’t that straight forward! ! ! – !
  • 17. JavaZone 2012 Object header Every object state on heap, also includes an object header Two words big mark word : bitfields for stuff (including synchronization state) klass pointer : pointer to another object (a meta-object) ! – – – 17
  • 18. JavaZone 2012 2 words in size? These are our missing 8 bytes! But wait... are these always twice 4 bytes? What about 64 bit systems? Are these arch words? These should, let’s try … ! ! ! – – 18
  • 19. Second conclusion demo … this better works this time!
  • 20. JavaZone 2012 Almost there Looks like removing a byte doesn’t buy us one byte … even more, adding one byte consumes more than expected … more so, on 64 bit arch, we’re really not getting what we expected! … there seems to be yet more to it. ! – – – 20
  • 21. JavaZone 2012 Memory alignment Aligning addresses in memory So a new address only starts at every X bytes CPU limitations Cache line faulting / invalidation The VM will (generally) align on 8 bytes JRockit does sometimes 16 bytes ! – – – ! – 21
  • 22. JavaZone 2012 … what about 64 bit arch? compressed oops 64 bit address space? Are you really using this? If feasible, the VM will represent managed pointers as 32-bit values Avoiding wasting too much memory when not required Still allowing applications to address up to four billion objects (not bytes!), or a heap size of up to about 32Gb ! ! – – – – 22
  • 23. JavaZone 2012 Right … pointers! References to other objects are effectively pointers A pointer will differ in size Depending on the arch Whether Compressed OOPs are used or not ! ! – – 23
  • 26. JavaZone 2012 The truth about class layouting 26 VM will layout the instance’s state in memory Not necessarily the way you’ve declared fields in the class Avoiding wasting memory ! ! –
  • 27. JavaZone 2012 Here’s what going on… That information is kept by the VM Referenced by the klass pointer The VM first lays out 64 bit, 32 bit, 16 bit, 8 bit primitives and finally OOP … but also backpads ! – ! – – 27
  • 30. JavaZone 2012 sun.misc.Unsafe.theUnsafe 30 Sun Oracle VM specific Lets you do bunch of … unsafe stuff ! !
  • 31. JavaZone 2012 java.util.concurrent.Atomic* how AtomicInteger uses Unsafe internally :! 31 10: public final boolean compareAndSet(int expect, int update) { 11: return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 12: } static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } }
  • 32. JavaZone 2012 UnsafeSizeOf Looking at the “last” field’s offset Math is easily done Still have to take care about the previous concerns Object Alignment ! – – • 32
  • 34. JavaZone 2012 JVMTI 34 java.lang.instrument.Instrumentation has getObjectSize(Object) calls into the VM to figure the memory footprint of an instance and … … returns it in bytes … … that’s it! ! – ! – – –
  • 35. JavaZone 2012 Now that sounds like … cool ! All we need is an instance of java.lang.instrument.Instrumentation … which is an interface … implemented by sun.instrument.InstrumentationImpl … which has a private constructor The way to get such an instance is … A Java Agent ! – – – ! – 35
  • 36. JavaZone 2012 Java Agent All we need is package it properly … with an MANIFEST.MF file in the jar declaring an Premain-Class The class needs a premain method public static void premain(String agentArgs, Instrumentation inst); ! – – ! – • 36
  • 37. Java Agent? … as in -javaagent: ?! You’re kidding me, right?
  • 38. JavaZone 2012 agentmain invoked by the VM when the agent is loaded … … the agent is loaded by providing a -javaagent:<locationToAgentJar> argument at VM startup Modifying VM startup scripts / arguments is painful in … real life We couldn’t settle for that! ! – ! ! 38
  • 39. JavaZone 2012 Introducing the AttachAPI Introduced in JDK6 Let’s you attach to a VM process, while it is running Including yourself … and load an agent (jar) Additional agentmain method required Additional MANIFEST.MF’s Agent-Class ! ! – ! – – 39
  • 40. JavaZone 2012 … now, where is it ? The AttachAPI is part of the tools.jar Which is on the classpath … … on OS X … but not on Windows, nor Linux But it’s there … … as part of the install ! ! – – ! – 40
  • 41. JavaZone 2012 Loading the AttachAPI Since it is not on the classpath … but present We load it ourselves … search for it “around” JAVA_HOME … if we find it load it ! And finally attach, load the agent… and get the Instrumentation instance! ! – ! – – ! 41
  • 42. Victory ! … and should that fail, we fall back to Unsafe, … … and if not present, fallback to Reflection based one
  • 43. Gotcha! You didn’t expect it to be that simple, did you?
  • 44. JavaZone 2012 One last thing ! There is one gotcha … Remember the GC we talked about? It needs to store some information too … … sometimes that information has a minimal footprint … like with CMS! ! ! – – – 44
  • 45. JavaZone 2012 HotSpot CMS’ FreeChunk 1: class FreeChunk VALUE_OBJ_CLASS_SPEC { 2: friend class VMStructs; 3: // For 64 bit compressed oops, the markOop encodes both the size and the 4: // indication that this is a FreeChunk and not an object. 5: volatile size_t _size; 6: FreeChunk* _prev; 7: FreeChunk* _next; (...) } 45
  • 46. Now we’re there … … but why is that cool again?
  • 47. JavaZone 2012 Why should I still care? The VM manages a lot of burden for us … But we still need to manage our heap size … heap size that can grow pretty large! But what should we do with all this memory? ! ! – ! 47
  • 48. JavaZone 2012 Caching Make your app faster! Keep data close to the processing unit … … no, that’s not the DB, but in memory! Make use of the large heap sizes VM offer today Letting the cache manage the resources ! – – ! ! 48
  • 49. JavaZone 2012 Ehcache ARC Automatic Resource Control Lets you size cache sizes in bytes, … … rather than in elements count (when I say byte, I mean byte, kb, mb, gb, tb) Not only caches but even all your caches ! ! – – – ! 49
  • 50. JavaZone 2012 Adding to the cache cache.put( new Element(key, value) ) Will size the the value (Object graph) the key (Object graph) the container(s) ! ! – – – 50
  • 51. JavaZone 2012 How much would you like? Ehcache has CacheManagers who manages … Caches Define your Cache or whole CacheManager sizes in maxBytesLocalHeap ! – – ! – 51
  • 52. JavaZone 2012 ehcache.xml <ehcache maxBytesLocalHeap="512m"> <cache name="CacheOne" /> <cache name="CacheTwo" /> <cache name="CacheThree" maxBytesLocalHeap="256m" /> </ehcache> 52
  • 53. It’s that easy … … thanks to all the rest we’ve covered!
  • 54. JavaZone 2012 Conclusion … that’s not all, there is more to all that. But it’s open source so go and see There might still be bugs! Bunch of VM options & optimizations to account for We might have missed some So, please, go download and try it out! ! – ! – – – 54
  • 55. Questions? Using SizeOf with ehcache, go visit www.ehcache.org