1) The document discusses different techniques for measuring the memory size of Java objects, including reflection, sun.misc.Unsafe, and JVMTI.
2) It explains that the memory consumed by an object is more than just the size of its fields due to object headers, alignment padding, and class metadata.
3) The techniques covered allow more accurately measuring object sizes, which can help optimize caching by allowing cache sizes to be set in bytes rather than object counts.
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
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
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
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
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
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
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
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
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
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
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