SlideShare a Scribd company logo
JVM: Memory
Management Details
Balaji Iyengar

Senior Software Engineer, Azul Systems
Presenter
• Balaji Iyengar
─ JVM Engineer at Azul Systems for the past 5+ years.
─ Currently a part-time PhD student.
─ Research in concurrent garbage collection.

©2011 Azul Systems, Inc.

2
Agenda
•
•
•
•
•
•

What is a JVM?
JVM Components

JVM Concepts/Terminology
Garbage Collection Basics
Concurrent Garbage Collection

Tools for analyzing memory issues

©2011 Azul Systems, Inc.

3
What is a Java Virtual Machine?
• Abstraction is the driving principal in the Java
Language specification
─ Bytecodes => processor instruction set
─ Java memory model => hardware memory model
─ Java threading model => OS threading model

• Abstract the ‘underlying’ platform details and
provide a standard development environment

©2011 Azul Systems, Inc.

4
What is a Java Virtual Machine?
• Java Virtual Machine along with a set of tools
implements the Java Language Specification
─ Bytecodes
─ Generated by the Javac compiler
─ Translated to the processor instruction set by the

JVM
─ Java threads
─ Mapped to OS threads by the JVM
─ Java memory model
─ JVM inserts the right ‘memory barriers’ when
needed
©2011 Azul Systems, Inc.

5
What is a Java Virtual Machine (JVM)?

• Layer between platform and the
application

• Abstracts away operating
system details

• Abstracts away hardware

Java
Application
C++
Application
Operating
System

Operating
System

Hardware

Hardware

JVM

architecture details

• Key to Java’s ‘write once run
anywhere’ capability.

©2011 Azul Systems, Inc.

6
Portability
Compile once, run everywhere
Same code!

Java
Application

Java
Application

JVM

JVM

Operating
System

Operating
System

Hardware
Architecture #1

©2011 Azul Systems, Inc.

Hardware
Architecture #2

7
The JVM Components
• An Interpreter
─ Straightforward translation from byte-codes to hardware

instructions
─ One byte-code at a time
─ No optimizations, simple translation engine

• JIT Compilers
─ Compiles byte-codes to hardware instructions
─ A lot more optimizations
─ Two different flavors targeting different optimizations
─ Client compiler for short running applications
─ Server compiler for long running applications
─ Server compiler generates more optimized code

©2011 Azul Systems, Inc.

8
The JVM Components
• A Runtime environment
─ Implements a threading model
 Creates and manages Java threads
 Each thread maps to an OS thread
─ Implements synchronization primitives, i.e., locks
─ Implements dynamic class loading & unloading
─ Implements features such as Reflection
─ Implements support for tools

©2011 Azul Systems, Inc.

9
The JVM Components
• Memory management module
─ Manages all of the program memory
─ Handles allocation requests
─ Recycles unused memory
Free
Memory
Garbage Collection

Unused
Memory

©2011 Azul Systems, Inc.

Allocation

Memory In
Use

Program Activity

10
JVM Concepts/Terminology
• Java Threads
─ Threads spawned by the application
─ Threads come and go during the life of a program
─ JVM allocates and cleanups resources on thread creation

and death
─ Each thread has a stack and several thread-local data
structures, i.e., execution context
─ Also referred to as ‘mutators’ since it mutates heap objects

©2011 Azul Systems, Inc.

11
JVM Concepts/Terminology
• Java objects
─
─
─
─

Java is an object oriented language 
Each allocation creates an object in memory
The JVM adds meta-data to each object: “object-header”
Object-header information useful for GC, synchronization, etc.

• Object Reference
─ Pointer to a Java object
─ Present in thread-stacks, registers, other heap objects
─ Top bits in a reference can be used for meta-data

©2011 Azul Systems, Inc.

12
JVM Concepts/Terminology
• Safepoints
─ The JVM has the ability to stop all Java threads
─ Used as a barrier mechanism between the JVM and the Java

threads
– ‘Safe’ place in code
• Function calls
• Backward branches
─ JVM has precise knowledge about mutator stacks/registers etc.
at a safepoint.
– Useful for GC purposes, e.g., STW GC happens at a safepoint.

Safepoints reflect as application ‘pauses’

©2011 Azul Systems, Inc.

13
Garbage Collection Taxonomy
•
•
•
•

Has been around for over 40 years in academia
For over 10 years in the enterprise

Identifies ‘live’ memory and recycles the ‘dead’ memory
Part of the memory management module in the JVM.

©2011 Azul Systems, Inc.

14
Garbage Collection Taxonomy
• Several ways to skin this cat:
–
–
–
–
–
–
–

Stop-The-World vs. Concurrent
Generational vs. Full Heap
Mark vs. Reference counting
Sweep vs. Compacting
Real Time vs. Non Real Time
Parallel vs. Single-threaded GC
Dozens of mechanisms
• Read-barriers
• Write-barriers
• Virtual memory tricks, etc..

©2011 Azul Systems, Inc.

15
Garbage Collection Taxonomy
• Stop-The-World GC
─ Recycles memory at safepoints only.

• Concurrent GC
─

Recycles memory without stopping mutators

• Generational GC
─ Divide the heap into smaller age-based regions
─ Empirically known that most garbage is found in ‘younger’

regions
─ Focus garbage collection work on ‘younger’ regions

©2011 Azul Systems, Inc.

16
Garbage Collection Basics
• What is ‘live’ memory
─ Liveness == Accessibility
─ Objects that can be directly or transitively accessed by mutators
─ Objects with pointers in mutator execution contexts, i.e., ‘root-

set’
─ Objects that can be reached via the root-set
─ Implemented using ‘mark’ or by ‘reference counting’

• What is ‘dead’ memory
─ Everything that is not ‘live’

©2011 Azul Systems, Inc.

17
Garbage Collection
• How does the garbage collector identify ‘live’ memory
─ Starts from the root set of mutator threads
─ Does a depth-first or breadth-first walk of the object graph
─ ‘Marks’ each object that is found, i.e., sets a bit in a liveness

bitmap
─ Referred to as the ‘mark-phase’
─ Could use reference counting
─ Problems with cyclic garbage
─ Problems with fragmentation

A

D
B

C

©2011 Azul Systems, Inc.

E

18
Garbage Collection Basics
• How does GC recycle ‘dead’ memory
Sweep:
─ Sweep ‘dead’ memory blocks into free-lists sorted by size
─ Hand out the right sized blocks to allocation requests
─ Pros:
─ Easy to do without stopping mutator threads
─ Cons
─ Slows down allocation path, reduces throughput
─ Can causes fragmentation

©2011 Azul Systems, Inc.

19
Garbage Collection Basics
• How does GC recycle ‘dead’ memory
Compaction:
Copy ‘live’ memory blocks into contiguous memory locations
Update pointers to old-locations
Recycle the original memory locations of live objects
Pros:
─ Supports higher allocation rates, i.e., higher throughputs
─ Gets rid of memory fragmentation
─ Cons: Concurrent versions are hard to get right
─
─
─
─

©2011 Azul Systems, Inc.

20
Garbage Collection
• Desired Characteristics
–
–
–
–

Concurrent
Compacting
Low application overhead
Scalable to large heaps

• These map best to current application characteristics
• These map best to current multi-core hardware

©2011 Azul Systems, Inc.

21
Concurrent Garbage Collection
• GC works in two phases
─ Mark Phase
─ Recycle Phase (Sweep/Compacting)

• Either one or both phases can be concurrent with mutator
threads

• Different set of problems to implement the two phases
concurrently

• GC needs to synchronize with application threads

©2011 Azul Systems, Inc.

22
Concurrent Garbage Collection
• Synchronization mechanisms between GC and mutators
Read Barrier
– Synchronization mechanism between GC and mutators
– Implemented only in code executed by the mutator
– Instruction or a set of instructions that follow a load of an object
–
–
–
–
–

reference
JIT compiler spits out the ‘read-barrier’
Precedes ‘use’ of the loaded reference.
Used to check GC invariants on the loaded reference
Expensive because of the frequency of reads
Functionality depends on the ‘algorithm’

©2011 Azul Systems, Inc.

23
Concurrent Garbage Collection
• Synchronization mechanisms between GC and mutators
Write Barrier
─
─
─
─
─
─
─

Similar to read-barrier
Implemented only in code executed by the mutator
Instruction or a set of instructions that follow/precede a write
JIT compiler spits out the ‘write-barrier’
Generally used to track pointer writes
Cheaper, since writes are less common
Functionality depends on the ‘algorithm’

©2011 Azul Systems, Inc.

24
Concurrent Garbage Collection
• Concurrent Mark
─ Scanning the heap graph while mutators are actively

changing it
─ Multiple-readers, single-writer coherence problem
─ Mutators are the multiple writers
─ GC only needs to read the graph structure

©2011 Azul Systems, Inc.

25
Concurrent Garbage Collection
• Concurrent Mark: What can go wrong?
Mutator writes a pointer to a yet ‘unseen’ object into an
object already ‘marked-through’ by GC
• Can be caught by write barriers
A
• Can be caught by read barriers as well
•

Mutator write

• GC considers object C ‘dead’.
• Will recycle object C, causing a crash
• Avoid by:
• Marking object C ‘live’ OR
• Re-traverse object A

C

B

Unmarked
Marked
Marked-Through

©2011 Azul Systems, Inc.

26
Concurrent Garbage Collection
• Concurrent Compaction: What can go wrong
─ Concurrent writes to old locations of objects can be lost
A
4
5
6

A’
0
0
0

Start Copy
•
•
•
•

A
4
5
6

A’
4
0
0

A
8
5
6

A’
4
5
0

A
8
5
6

A’
4
5
6

Mutator Write End Copy

Timeline

Object A is being copied to new location A’
A is the ‘From-Object’ ; A’ is the To-Object
Mutator writes to ‘From-Object’ field after it has been copied
Happens because mutator still holds a pointer to ‘From-Object’

Need to make sure that writes to object A, during and after the copy are reflected in
the new location A’

©2011 Azul Systems, Inc.

27
Concurrent Garbage Collection
Concurrent Compaction: What can go wrong
Propagating pointers to the old-location
B

B
A

C

D

A’

A

Relocate A

C

D
E

During or after the object copy is done, the mutator writes a
pointer to the old-location of the object in an object that is not
known to the collector
©2011 Azul Systems, Inc.

28
Concurrent Garbage Collection
• Propagating pointers to the old-location
─ Collector thinks object A has been copied to A’
─ Recycles old-location A
─ Mutator attempts to access A via object E and crashes

• Can be prevented by using
─ Read barriers, e.g., Azul’s C4 Collector
─ Compacting in ‘stop-the-world’ mode, e.g., CMS Collector

©2011 Azul Systems, Inc.

29
Biggest Java Scalability Limitation
• For MOST JVMs, compaction pauses are the biggest
current challenge and key limiting factor to Java
scalability

• The larger heap and live data / references to follow, the
bigger challenge for compaction

• Today: most JVMs limited to 3-4GB
─
─
─
─

To keep “FullGC” pause times within SLAs
Design limitations to make applications survive in 4GB chunks
Horizontal scale out / clustering solutions
In spite of machine memory increasing over the years…

 This is why I find Zing so interesting, as it has implemented concurrent
compaction…
─ But that is not the topic of this presentation… 

©2011 Azul Systems, Inc.

30
Tools: Memory Usage

©2011 Azul Systems, Inc.

31
Tools: Memory Usage Increasing

©2011 Azul Systems, Inc.

32
Tools: jmap
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [server_id@]<remote server IP or hostname>
(to connect to remote debug server)
where <option> is one of:
<none>
to print same info as Solaris pmap
-heap
to print java heap summary
-histo[:live]
to print histogram of java object heap; if the "live"
suboption is specified, only count live objects
-permstat
to print permanent generation statistics
-finalizerinfo
to print information on objects awaiting finalization
-dump:<dump-options> to dump java heap in hprof binary format
dump-options:
live
dump only live objects; if not specified,
all objects in the heap are dumped.
format=b binary format
file=<file> dump heap to <file>
Example: jmap -dump:live,format=b,file=heap.bin <pid>
-F
force. Use with -dump:<dump-options> <pid> or -histo
to force a heap dump or histogram when <pid> does not
respond. The "live" suboption is not supported
in this mode.
-h | -help
to print this help message
-J<flag>
to pass <flag> directly to the runtime system

©2011 Azul Systems, Inc.

33
Tools: jmap Command to Collect

/jdk6_23/bin/jmap -dump:live,file=SPECjbb2005_2_warehouses 15395
File sizes
-rw-------. 1 me users 86659277 2011-06-15 15:23 SPECjbb2005_2_warehouses.hprof
-rw-------. 1 me users 480108823 2011-06-15 15:25 SPECjbb2005_12_warehouses.hprof

©2011 Azul Systems, Inc.

34
Tools: JProfiler Memory Snapshot

©2011 Azul Systems, Inc.

35
Tools: JProfiler Objects (2 warehouses)

©2011 Azul Systems, Inc.

36
Tools: JProfiler Biggest Retained Sets

©2011 Azul Systems, Inc.

37
Tools: JProfiler Objects (12 warehouses)

©2011 Azul Systems, Inc.

38
Tools: JProfiler Biggest Retained Sets

©2011 Azul Systems, Inc.

39
Tools: JProfiler Difference Between 2/12

©2011 Azul Systems, Inc.

40
Tools: madmap

©2011 Azul Systems, Inc.

41
GC and Tool Support
• The Heap dump tools uses the GC interface
─ Walks the object graph using the same mechanism as GC
─ Writes out per-object data to a file that can later be analyzed.

• GC also outputs detailed logs
─ These are very useful in identifying memory related bottle necks
─ Quite a few tools available to analyze GC logs

©2011 Azul Systems, Inc.

42
2c for the Road
What to (not) Think About

1. Why not use multiple threads, when you can?
─

Number of cores per server continues to grow…

2. Don’t be afraid of garbage, it is good!
3. I personally don’t like finalizers…error prone, not
guaranteed to run (resource wasting)

4. Always be careful around locking
─

If it passes testing, hot locks can still block during production load

5. Benchmarks are often focused on throughput, but miss out
on real GC impact – test your real application!
─
─

“Full GC” never occurs during the run, not running long enough to
see impact of fragmentation
Response time std dev and outliers (99.9…%) are of importance
for a real world app, not throughput alone!!

©2011 Azul Systems, Inc.

43
Summary
• JVM – a great abstraction, provides convenient services
so the Java programmer doesn’t have to deal with
environment specific things

• Compiler – “intelligent and context-aware translator” who
helps speed up your application

• Garbage Collector – simplifies memory management,
different flavors for different needs

• Compaction – an inevitable task, which impact grows with
live size and data complexity for most JVMs, and the
current largest limiter of Java Scalability

©2011 Azul Systems, Inc.

44
For the Curious: What is Zing?
• Azul Systems has developed scalable Java platforms for
8+ years
─ Vega product line based on proprietary chip architecture, kernel

enhancements, and JVM innovation
─ Zing product line based on x86 chip architecture, virtualization
and kernel enhancements, and JVM innovation

• Most famous for our Generational Pauseless Garbage
Collector, which performs fully concurrent compaction

©2011 Azul Systems, Inc.

45
Q&A

balaji@azulsystems.com
http://twitter.com/AzulSystemsPM
www.azulsystems.com/zing

©2011 Azul Systems, Inc.

46
Additional Resources
• For more information on…
…JDK internals: http://openjdk.java.net/ (JVM source code)
…Memory management:
http://java.sun.com/j2se/reference/whitepapers/memorymanagement_w
hitepaper.pdf (a bit old, but very comprehensive)
…Tuning:
http://download.oracle.com/docs/cd/E13150_01/jrockit_jvm/jrockit/genin
fo/diagnos/tune_stable_perf.html (watch out for increased rigidity and
re-tuning pain)
…Generational Pauseless Garbage Collection:
http://www.azulsystems.com/webinar/pauseless-gc (webinar by Gil
Tene, 2011)
…Compiler internals and optimizations:
http://www.azulsystems.com/blogs/cliff (Dr Cliff Click’s blog)

©2011 Azul Systems, Inc.

47

More Related Content

What's hot (20)

PDF
Secrets of Performance Tuning Java on Kubernetes
Bruno Borges
 
PDF
Java collections
Hamid Ghorbani
 
PPTX
Design pattern (Abstract Factory & Singleton)
paramisoft
 
PPTX
Apache Spark Architecture
Alexey Grishchenko
 
PDF
Parquet performance tuning: the missing guide
Ryan Blue
 
PPSX
Collections - Lists, Sets
Hitesh-Java
 
PPSX
Elements of Java Language
Hitesh-Java
 
PDF
Introduction to Cassandra
Gokhan Atil
 
PDF
Python Collections Tutorial | Edureka
Edureka!
 
PDF
How to Manage Scale-Out Environments with MariaDB MaxScale
MariaDB plc
 
PPTX
Flask – Python
Max Claus Nunes
 
PPTX
Java project-presentation
APSMIND TECHNOLOGY PVT LTD.
 
PDF
Arrays in Java
Naz Abdalla
 
PDF
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
Arawn Park
 
PPTX
Monitor và tối ưu sql server
Huân Bùi Đình
 
PDF
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
confluent
 
PPT
Basic Garbage Collection Techniques
An Khuong
 
PPT
Collection Framework in java
CPD INDIA
 
PDF
Apache Lucene/Solr Document Classification
Sease
 
KEY
Everything I Ever Learned About JVM Performance Tuning @Twitter
Attila Szegedi
 
Secrets of Performance Tuning Java on Kubernetes
Bruno Borges
 
Java collections
Hamid Ghorbani
 
Design pattern (Abstract Factory & Singleton)
paramisoft
 
Apache Spark Architecture
Alexey Grishchenko
 
Parquet performance tuning: the missing guide
Ryan Blue
 
Collections - Lists, Sets
Hitesh-Java
 
Elements of Java Language
Hitesh-Java
 
Introduction to Cassandra
Gokhan Atil
 
Python Collections Tutorial | Edureka
Edureka!
 
How to Manage Scale-Out Environments with MariaDB MaxScale
MariaDB plc
 
Flask – Python
Max Claus Nunes
 
Java project-presentation
APSMIND TECHNOLOGY PVT LTD.
 
Arrays in Java
Naz Abdalla
 
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
Arawn Park
 
Monitor và tối ưu sql server
Huân Bùi Đình
 
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
confluent
 
Basic Garbage Collection Techniques
An Khuong
 
Collection Framework in java
CPD INDIA
 
Apache Lucene/Solr Document Classification
Sease
 
Everything I Ever Learned About JVM Performance Tuning @Twitter
Attila Szegedi
 

Similar to JVM Memory Management Details (20)

PDF
Understanding Zulu Garbage Collection by Matt Schuetze, Director of Product M...
zuluJDK
 
PDF
Understanding Java Garbage Collection
Azul Systems Inc.
 
PDF
What's Inside a JVM?
Azul Systems Inc.
 
PDF
Garbage Collection in Java.pdf
SudhanshiBakre1
 
PDF
JVM Mechanics: A Peek Under the Hood
Azul Systems Inc.
 
PDF
Understanding GC, JavaOne 2017
Azul Systems Inc.
 
PPTX
Garbage collection
Anand Srinivasan
 
PDF
Understanding Java Garbage Collection - And What You Can Do About It
Azul Systems Inc.
 
PDF
Advancements ingc andc4overview_linkedin_oct2017
Azul Systems Inc.
 
PDF
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
PDF
Java at Scale, Dallas JUG, October 2013
Azul Systems Inc.
 
PDF
Jvm is-your-friend
ColdFusionConference
 
PDF
The JVM is your friend
Kai Koenig
 
PPT
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
ODP
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
PPTX
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
ODP
Gc algorithms
Michał Warecki
 
PPTX
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
PPTX
#JavaOne What's in an object?
Charlie Gracie
 
PPTX
JavaOne2015-What's in an Object?
Charlie Gracie
 
Understanding Zulu Garbage Collection by Matt Schuetze, Director of Product M...
zuluJDK
 
Understanding Java Garbage Collection
Azul Systems Inc.
 
What's Inside a JVM?
Azul Systems Inc.
 
Garbage Collection in Java.pdf
SudhanshiBakre1
 
JVM Mechanics: A Peek Under the Hood
Azul Systems Inc.
 
Understanding GC, JavaOne 2017
Azul Systems Inc.
 
Garbage collection
Anand Srinivasan
 
Understanding Java Garbage Collection - And What You Can Do About It
Azul Systems Inc.
 
Advancements ingc andc4overview_linkedin_oct2017
Azul Systems Inc.
 
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
Java at Scale, Dallas JUG, October 2013
Azul Systems Inc.
 
Jvm is-your-friend
ColdFusionConference
 
The JVM is your friend
Kai Koenig
 
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Gc algorithms
Michał Warecki
 
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
#JavaOne What's in an object?
Charlie Gracie
 
JavaOne2015-What's in an Object?
Charlie Gracie
 
Ad

More from Azul Systems Inc. (20)

PPTX
Zulu Embedded Java Introduction
Azul Systems Inc.
 
PDF
What's New in the JVM in Java 8?
Azul Systems Inc.
 
PDF
DotCMS Bootcamp: Enabling Java in Latency Sensitivie Environments
Azul Systems Inc.
 
PDF
ObjectLayout: Closing the (last?) inherent C vs. Java speed gap
Azul Systems Inc.
 
PDF
Priming Java for Speed at Market Open
Azul Systems Inc.
 
PDF
Azul Systems open source guide
Azul Systems Inc.
 
PPTX
Start Fast and Stay Fast - Priming Java for Market Open with ReadyNow!
Azul Systems Inc.
 
PDF
Intelligent Trading Summit NY 2014: Understanding Latency: Key Lessons and Tools
Azul Systems Inc.
 
PDF
The evolution of OpenJDK: From Java's beginnings to 2014
Azul Systems Inc.
 
PDF
Push Technology's latest data distribution benchmark with Solarflare and Zing
Azul Systems Inc.
 
PDF
Webinar: Zing Vision: Answering your toughest production Java performance que...
Azul Systems Inc.
 
PPT
Speculative Locking: Breaking the Scale Barrier (JAOO 2005)
Azul Systems Inc.
 
PDF
Java vs. C/C++
Azul Systems Inc.
 
PDF
The Java Evolution Mismatch - Why You Need a Better JVM
Azul Systems Inc.
 
PDF
Towards a Scalable Non-Blocking Coding Style
Azul Systems Inc.
 
PDF
Experiences with Debugging Data Races
Azul Systems Inc.
 
PDF
Lock-Free, Wait-Free Hash Table
Azul Systems Inc.
 
PDF
How NOT to Write a Microbenchmark
Azul Systems Inc.
 
PDF
The Art of Java Benchmarking
Azul Systems Inc.
 
PDF
Azul Zulu on Azure Overview -- OpenTech CEE Workshop, Warsaw, Poland
Azul Systems Inc.
 
Zulu Embedded Java Introduction
Azul Systems Inc.
 
What's New in the JVM in Java 8?
Azul Systems Inc.
 
DotCMS Bootcamp: Enabling Java in Latency Sensitivie Environments
Azul Systems Inc.
 
ObjectLayout: Closing the (last?) inherent C vs. Java speed gap
Azul Systems Inc.
 
Priming Java for Speed at Market Open
Azul Systems Inc.
 
Azul Systems open source guide
Azul Systems Inc.
 
Start Fast and Stay Fast - Priming Java for Market Open with ReadyNow!
Azul Systems Inc.
 
Intelligent Trading Summit NY 2014: Understanding Latency: Key Lessons and Tools
Azul Systems Inc.
 
The evolution of OpenJDK: From Java's beginnings to 2014
Azul Systems Inc.
 
Push Technology's latest data distribution benchmark with Solarflare and Zing
Azul Systems Inc.
 
Webinar: Zing Vision: Answering your toughest production Java performance que...
Azul Systems Inc.
 
Speculative Locking: Breaking the Scale Barrier (JAOO 2005)
Azul Systems Inc.
 
Java vs. C/C++
Azul Systems Inc.
 
The Java Evolution Mismatch - Why You Need a Better JVM
Azul Systems Inc.
 
Towards a Scalable Non-Blocking Coding Style
Azul Systems Inc.
 
Experiences with Debugging Data Races
Azul Systems Inc.
 
Lock-Free, Wait-Free Hash Table
Azul Systems Inc.
 
How NOT to Write a Microbenchmark
Azul Systems Inc.
 
The Art of Java Benchmarking
Azul Systems Inc.
 
Azul Zulu on Azure Overview -- OpenTech CEE Workshop, Warsaw, Poland
Azul Systems Inc.
 
Ad

Recently uploaded (20)

PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PPTX
Essential Content-centric Plugins for your Website
Laura Byrne
 
PDF
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
Essential Content-centric Plugins for your Website
Laura Byrne
 
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Securing Model Context Protocol with Keycloak: AuthN/AuthZ for MCP Servers
Hitachi, Ltd. OSS Solution Center.
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 

JVM Memory Management Details

  • 1. JVM: Memory Management Details Balaji Iyengar Senior Software Engineer, Azul Systems
  • 2. Presenter • Balaji Iyengar ─ JVM Engineer at Azul Systems for the past 5+ years. ─ Currently a part-time PhD student. ─ Research in concurrent garbage collection. ©2011 Azul Systems, Inc. 2
  • 3. Agenda • • • • • • What is a JVM? JVM Components JVM Concepts/Terminology Garbage Collection Basics Concurrent Garbage Collection Tools for analyzing memory issues ©2011 Azul Systems, Inc. 3
  • 4. What is a Java Virtual Machine? • Abstraction is the driving principal in the Java Language specification ─ Bytecodes => processor instruction set ─ Java memory model => hardware memory model ─ Java threading model => OS threading model • Abstract the ‘underlying’ platform details and provide a standard development environment ©2011 Azul Systems, Inc. 4
  • 5. What is a Java Virtual Machine? • Java Virtual Machine along with a set of tools implements the Java Language Specification ─ Bytecodes ─ Generated by the Javac compiler ─ Translated to the processor instruction set by the JVM ─ Java threads ─ Mapped to OS threads by the JVM ─ Java memory model ─ JVM inserts the right ‘memory barriers’ when needed ©2011 Azul Systems, Inc. 5
  • 6. What is a Java Virtual Machine (JVM)? • Layer between platform and the application • Abstracts away operating system details • Abstracts away hardware Java Application C++ Application Operating System Operating System Hardware Hardware JVM architecture details • Key to Java’s ‘write once run anywhere’ capability. ©2011 Azul Systems, Inc. 6
  • 7. Portability Compile once, run everywhere Same code! Java Application Java Application JVM JVM Operating System Operating System Hardware Architecture #1 ©2011 Azul Systems, Inc. Hardware Architecture #2 7
  • 8. The JVM Components • An Interpreter ─ Straightforward translation from byte-codes to hardware instructions ─ One byte-code at a time ─ No optimizations, simple translation engine • JIT Compilers ─ Compiles byte-codes to hardware instructions ─ A lot more optimizations ─ Two different flavors targeting different optimizations ─ Client compiler for short running applications ─ Server compiler for long running applications ─ Server compiler generates more optimized code ©2011 Azul Systems, Inc. 8
  • 9. The JVM Components • A Runtime environment ─ Implements a threading model  Creates and manages Java threads  Each thread maps to an OS thread ─ Implements synchronization primitives, i.e., locks ─ Implements dynamic class loading & unloading ─ Implements features such as Reflection ─ Implements support for tools ©2011 Azul Systems, Inc. 9
  • 10. The JVM Components • Memory management module ─ Manages all of the program memory ─ Handles allocation requests ─ Recycles unused memory Free Memory Garbage Collection Unused Memory ©2011 Azul Systems, Inc. Allocation Memory In Use Program Activity 10
  • 11. JVM Concepts/Terminology • Java Threads ─ Threads spawned by the application ─ Threads come and go during the life of a program ─ JVM allocates and cleanups resources on thread creation and death ─ Each thread has a stack and several thread-local data structures, i.e., execution context ─ Also referred to as ‘mutators’ since it mutates heap objects ©2011 Azul Systems, Inc. 11
  • 12. JVM Concepts/Terminology • Java objects ─ ─ ─ ─ Java is an object oriented language  Each allocation creates an object in memory The JVM adds meta-data to each object: “object-header” Object-header information useful for GC, synchronization, etc. • Object Reference ─ Pointer to a Java object ─ Present in thread-stacks, registers, other heap objects ─ Top bits in a reference can be used for meta-data ©2011 Azul Systems, Inc. 12
  • 13. JVM Concepts/Terminology • Safepoints ─ The JVM has the ability to stop all Java threads ─ Used as a barrier mechanism between the JVM and the Java threads – ‘Safe’ place in code • Function calls • Backward branches ─ JVM has precise knowledge about mutator stacks/registers etc. at a safepoint. – Useful for GC purposes, e.g., STW GC happens at a safepoint. Safepoints reflect as application ‘pauses’ ©2011 Azul Systems, Inc. 13
  • 14. Garbage Collection Taxonomy • • • • Has been around for over 40 years in academia For over 10 years in the enterprise Identifies ‘live’ memory and recycles the ‘dead’ memory Part of the memory management module in the JVM. ©2011 Azul Systems, Inc. 14
  • 15. Garbage Collection Taxonomy • Several ways to skin this cat: – – – – – – – Stop-The-World vs. Concurrent Generational vs. Full Heap Mark vs. Reference counting Sweep vs. Compacting Real Time vs. Non Real Time Parallel vs. Single-threaded GC Dozens of mechanisms • Read-barriers • Write-barriers • Virtual memory tricks, etc.. ©2011 Azul Systems, Inc. 15
  • 16. Garbage Collection Taxonomy • Stop-The-World GC ─ Recycles memory at safepoints only. • Concurrent GC ─ Recycles memory without stopping mutators • Generational GC ─ Divide the heap into smaller age-based regions ─ Empirically known that most garbage is found in ‘younger’ regions ─ Focus garbage collection work on ‘younger’ regions ©2011 Azul Systems, Inc. 16
  • 17. Garbage Collection Basics • What is ‘live’ memory ─ Liveness == Accessibility ─ Objects that can be directly or transitively accessed by mutators ─ Objects with pointers in mutator execution contexts, i.e., ‘root- set’ ─ Objects that can be reached via the root-set ─ Implemented using ‘mark’ or by ‘reference counting’ • What is ‘dead’ memory ─ Everything that is not ‘live’ ©2011 Azul Systems, Inc. 17
  • 18. Garbage Collection • How does the garbage collector identify ‘live’ memory ─ Starts from the root set of mutator threads ─ Does a depth-first or breadth-first walk of the object graph ─ ‘Marks’ each object that is found, i.e., sets a bit in a liveness bitmap ─ Referred to as the ‘mark-phase’ ─ Could use reference counting ─ Problems with cyclic garbage ─ Problems with fragmentation A D B C ©2011 Azul Systems, Inc. E 18
  • 19. Garbage Collection Basics • How does GC recycle ‘dead’ memory Sweep: ─ Sweep ‘dead’ memory blocks into free-lists sorted by size ─ Hand out the right sized blocks to allocation requests ─ Pros: ─ Easy to do without stopping mutator threads ─ Cons ─ Slows down allocation path, reduces throughput ─ Can causes fragmentation ©2011 Azul Systems, Inc. 19
  • 20. Garbage Collection Basics • How does GC recycle ‘dead’ memory Compaction: Copy ‘live’ memory blocks into contiguous memory locations Update pointers to old-locations Recycle the original memory locations of live objects Pros: ─ Supports higher allocation rates, i.e., higher throughputs ─ Gets rid of memory fragmentation ─ Cons: Concurrent versions are hard to get right ─ ─ ─ ─ ©2011 Azul Systems, Inc. 20
  • 21. Garbage Collection • Desired Characteristics – – – – Concurrent Compacting Low application overhead Scalable to large heaps • These map best to current application characteristics • These map best to current multi-core hardware ©2011 Azul Systems, Inc. 21
  • 22. Concurrent Garbage Collection • GC works in two phases ─ Mark Phase ─ Recycle Phase (Sweep/Compacting) • Either one or both phases can be concurrent with mutator threads • Different set of problems to implement the two phases concurrently • GC needs to synchronize with application threads ©2011 Azul Systems, Inc. 22
  • 23. Concurrent Garbage Collection • Synchronization mechanisms between GC and mutators Read Barrier – Synchronization mechanism between GC and mutators – Implemented only in code executed by the mutator – Instruction or a set of instructions that follow a load of an object – – – – – reference JIT compiler spits out the ‘read-barrier’ Precedes ‘use’ of the loaded reference. Used to check GC invariants on the loaded reference Expensive because of the frequency of reads Functionality depends on the ‘algorithm’ ©2011 Azul Systems, Inc. 23
  • 24. Concurrent Garbage Collection • Synchronization mechanisms between GC and mutators Write Barrier ─ ─ ─ ─ ─ ─ ─ Similar to read-barrier Implemented only in code executed by the mutator Instruction or a set of instructions that follow/precede a write JIT compiler spits out the ‘write-barrier’ Generally used to track pointer writes Cheaper, since writes are less common Functionality depends on the ‘algorithm’ ©2011 Azul Systems, Inc. 24
  • 25. Concurrent Garbage Collection • Concurrent Mark ─ Scanning the heap graph while mutators are actively changing it ─ Multiple-readers, single-writer coherence problem ─ Mutators are the multiple writers ─ GC only needs to read the graph structure ©2011 Azul Systems, Inc. 25
  • 26. Concurrent Garbage Collection • Concurrent Mark: What can go wrong? Mutator writes a pointer to a yet ‘unseen’ object into an object already ‘marked-through’ by GC • Can be caught by write barriers A • Can be caught by read barriers as well • Mutator write • GC considers object C ‘dead’. • Will recycle object C, causing a crash • Avoid by: • Marking object C ‘live’ OR • Re-traverse object A C B Unmarked Marked Marked-Through ©2011 Azul Systems, Inc. 26
  • 27. Concurrent Garbage Collection • Concurrent Compaction: What can go wrong ─ Concurrent writes to old locations of objects can be lost A 4 5 6 A’ 0 0 0 Start Copy • • • • A 4 5 6 A’ 4 0 0 A 8 5 6 A’ 4 5 0 A 8 5 6 A’ 4 5 6 Mutator Write End Copy Timeline Object A is being copied to new location A’ A is the ‘From-Object’ ; A’ is the To-Object Mutator writes to ‘From-Object’ field after it has been copied Happens because mutator still holds a pointer to ‘From-Object’ Need to make sure that writes to object A, during and after the copy are reflected in the new location A’ ©2011 Azul Systems, Inc. 27
  • 28. Concurrent Garbage Collection Concurrent Compaction: What can go wrong Propagating pointers to the old-location B B A C D A’ A Relocate A C D E During or after the object copy is done, the mutator writes a pointer to the old-location of the object in an object that is not known to the collector ©2011 Azul Systems, Inc. 28
  • 29. Concurrent Garbage Collection • Propagating pointers to the old-location ─ Collector thinks object A has been copied to A’ ─ Recycles old-location A ─ Mutator attempts to access A via object E and crashes • Can be prevented by using ─ Read barriers, e.g., Azul’s C4 Collector ─ Compacting in ‘stop-the-world’ mode, e.g., CMS Collector ©2011 Azul Systems, Inc. 29
  • 30. Biggest Java Scalability Limitation • For MOST JVMs, compaction pauses are the biggest current challenge and key limiting factor to Java scalability • The larger heap and live data / references to follow, the bigger challenge for compaction • Today: most JVMs limited to 3-4GB ─ ─ ─ ─ To keep “FullGC” pause times within SLAs Design limitations to make applications survive in 4GB chunks Horizontal scale out / clustering solutions In spite of machine memory increasing over the years…  This is why I find Zing so interesting, as it has implemented concurrent compaction… ─ But that is not the topic of this presentation…  ©2011 Azul Systems, Inc. 30
  • 31. Tools: Memory Usage ©2011 Azul Systems, Inc. 31
  • 32. Tools: Memory Usage Increasing ©2011 Azul Systems, Inc. 32
  • 33. Tools: jmap Usage: jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to connect to a core file) jmap [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server) where <option> is one of: <none> to print same info as Solaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -permstat to print permanent generation statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system ©2011 Azul Systems, Inc. 33
  • 34. Tools: jmap Command to Collect /jdk6_23/bin/jmap -dump:live,file=SPECjbb2005_2_warehouses 15395 File sizes -rw-------. 1 me users 86659277 2011-06-15 15:23 SPECjbb2005_2_warehouses.hprof -rw-------. 1 me users 480108823 2011-06-15 15:25 SPECjbb2005_12_warehouses.hprof ©2011 Azul Systems, Inc. 34
  • 35. Tools: JProfiler Memory Snapshot ©2011 Azul Systems, Inc. 35
  • 36. Tools: JProfiler Objects (2 warehouses) ©2011 Azul Systems, Inc. 36
  • 37. Tools: JProfiler Biggest Retained Sets ©2011 Azul Systems, Inc. 37
  • 38. Tools: JProfiler Objects (12 warehouses) ©2011 Azul Systems, Inc. 38
  • 39. Tools: JProfiler Biggest Retained Sets ©2011 Azul Systems, Inc. 39
  • 40. Tools: JProfiler Difference Between 2/12 ©2011 Azul Systems, Inc. 40
  • 41. Tools: madmap ©2011 Azul Systems, Inc. 41
  • 42. GC and Tool Support • The Heap dump tools uses the GC interface ─ Walks the object graph using the same mechanism as GC ─ Writes out per-object data to a file that can later be analyzed. • GC also outputs detailed logs ─ These are very useful in identifying memory related bottle necks ─ Quite a few tools available to analyze GC logs ©2011 Azul Systems, Inc. 42
  • 43. 2c for the Road What to (not) Think About 1. Why not use multiple threads, when you can? ─ Number of cores per server continues to grow… 2. Don’t be afraid of garbage, it is good! 3. I personally don’t like finalizers…error prone, not guaranteed to run (resource wasting) 4. Always be careful around locking ─ If it passes testing, hot locks can still block during production load 5. Benchmarks are often focused on throughput, but miss out on real GC impact – test your real application! ─ ─ “Full GC” never occurs during the run, not running long enough to see impact of fragmentation Response time std dev and outliers (99.9…%) are of importance for a real world app, not throughput alone!! ©2011 Azul Systems, Inc. 43
  • 44. Summary • JVM – a great abstraction, provides convenient services so the Java programmer doesn’t have to deal with environment specific things • Compiler – “intelligent and context-aware translator” who helps speed up your application • Garbage Collector – simplifies memory management, different flavors for different needs • Compaction – an inevitable task, which impact grows with live size and data complexity for most JVMs, and the current largest limiter of Java Scalability ©2011 Azul Systems, Inc. 44
  • 45. For the Curious: What is Zing? • Azul Systems has developed scalable Java platforms for 8+ years ─ Vega product line based on proprietary chip architecture, kernel enhancements, and JVM innovation ─ Zing product line based on x86 chip architecture, virtualization and kernel enhancements, and JVM innovation • Most famous for our Generational Pauseless Garbage Collector, which performs fully concurrent compaction ©2011 Azul Systems, Inc. 45
  • 47. Additional Resources • For more information on… …JDK internals: http://openjdk.java.net/ (JVM source code) …Memory management: http://java.sun.com/j2se/reference/whitepapers/memorymanagement_w hitepaper.pdf (a bit old, but very comprehensive) …Tuning: http://download.oracle.com/docs/cd/E13150_01/jrockit_jvm/jrockit/genin fo/diagnos/tune_stable_perf.html (watch out for increased rigidity and re-tuning pain) …Generational Pauseless Garbage Collection: http://www.azulsystems.com/webinar/pauseless-gc (webinar by Gil Tene, 2011) …Compiler internals and optimizations: http://www.azulsystems.com/blogs/cliff (Dr Cliff Click’s blog) ©2011 Azul Systems, Inc. 47