Native Memory Leak?
Download
Report
Transcript Native Memory Leak?
Java Memory Management Overview
Justin Chen
Mar 16rd, 2009
CONFIDENTIAL
2-Apr-16
1
Before Start …
Learning HOW is more important than learning WHAT.
知其然,更要知其所以然
CONFIDENTIAL
2-Apr-16
2
Agenda
• How Java Object Stores in Memory
• Shallow Size, Retained Size and Weak Reference
• JVM Memory Structure & Heap Dump
• How GC works
• GC Algorithm
• Hints on GC Turning
• VisualVM and GCViewer
• Out of Memory – 3 types
• Memory Leak, Permanent Memory Leak, Native Memory Leak
• Memory Leak & Eclipse Memory Analyzer
• JDK 6
CONFIDENTIAL
2-Apr-16
3
Size of Java Types & Objects
From Java Spec -
Actual Memory Size -
byte : 8-bit
short : 16-bit
Int : 32-bit
long : 64-bit
char : 16 bit unsigned integer
(depends on JVM Implementation)
Byte : 16 bytes
Short : 16 bytes
Why?
Integer : 16 bytes
Memory Layout of Boolean
Long : 16 bytes
[HEADER: 8 bytes] 8
Character : 16 bytes [value: 1 byte ] 9
float : 32-bit
double: 64-bit
boolean: 1 bit?
So, how about Object?
-new Object()
-new
[padding: 7 bytes] 16
Float : 16 bytes
Double : 16 bytes
Boolean : 16 bytes
new Object() => 8 bytes
new Double[] itself => 16 bytes
new ArrayList<Double> itself => 24 bytes
CONFIDENTIAL
2-Apr-16
4
Shallow and Retained Sizes
Shallow Size : the amount of allocated memory to store the object itself.
new ArrayList<Boolean>() = 24 bytes
Retained Size : shallow size + the shallow sizes of the objects that are accessible,
directly or indirectly, only from this object. In other words, the retained size
represents the amount of memory that will be freed by the garbage collector
when this object is collected.
What’s GC root?
new ArrayList<Boolean>() = 80 bytes
CONFIDENTIAL
2-Apr-16
5
Weak Reference
• When strong reference are too strong
– StringBuffer buffer = new StringBuffer();
– hashMap.put(key, value)
– cache.add(image)
• Non-Strong Reference
– An object referenced only by weak references is considered unreachable (or "weakly
reachable") and so may be collected at any time.
• Reference Strength: strong > soft > weak > phantom
• Soft Reference
– Mostly often used to implement memory-sensitive caches
– No constraints on GC time, but will be cleared before OOM
• Weak Reference
– Weakly reachable object will be discarded at the next GC cycle
• Phantom Reference
– An object is phantomly referenced after it has been finalized, but before its allocated
memory has been reclaimed.
CONFIDENTIAL
2-Apr-16
6
Heap Dump
• The heap dump is a dump of all the live objects and classes.
– Example: java_pid460.hprof.20090314.234647
• The ways to get Heap Dump (Sun Hotspot JVM 5.0 update 16)
– -XX:+HeapDumpOnOutOfMemoryError
– -XX:+HeapDumpOnCtrlBreak
– use profiling tool, such as VisualVM
• You need a tool to analyze heap dump
– Runtime Diagram, Visual VM
– Post-mortem analysis, Eclipse Memory Analyzer
CONFIDENTIAL
2-Apr-16
7
JVM Memory Structure
Eden
S0
All class instances
New/Young
Arrays
-Xmx<size> max New/Old heap size
S1
-Xms<size> initial New/Old heap size
User Heap
Old
(tenured)
GC-able
Heap?
per-class structures:
what is interned String?
run-time constant pool, field and method data
Permanent
codes for methods and constructors
Interned String
-XX:MaxPermSize
Native
The memory managed by OS
CONFIDENTIAL
2-Apr-16
8
How GC works? GC of Serial Collection
Eden
From
To
Empty
Empty
Survivor Spaces
Old
CONFIDENTIAL
2-Apr-16
9
How GC works? Full GC of Serial Collection
• Full GC: A old or permanent generation collection (Stop the world)
Before Full GC
After Full GC
CONFIDENTIAL
2-Apr-16
10
GC Algorithm
• Serial Collector
– as we described in previous slides
– default collector for client-class server
• Parallel Collector
– Performing the young generation collection in parallel, old generation is same
– default collector for server-class server
– -XX:+UseParallelGC
• Parallel Compacting Collector
– Performing the old generation collection in parallel comparing to parallec
collector
– Eventually, this collector will replace Parallel Collector in the future
– -XX:+UseParallelOldGC
• Concurrent Mark-Sweep (CMS) Collector (low-latency collector)
– Shorter GC pauses, but need larger heap space and may cause fragmentation
– -XX:+UseConcMarkSweepGC
CONFIDENTIAL
2-Apr-16
11
GC Algorithm Performance Metrics
• Throughput – High is better
– percentage of total time not spent in GC
• Garbage Collection Overhead
– percentage of total time spent in GC
• Pause Time – Low is better
– the length of Stop Time for GC
• Frequency of collection
– how often collection occurs
• Footprint
– a measure of size, such as heap size
• Promptness
– The time between an object becomes garbage and when the memory
becomes available
CONFIDENTIAL
2-Apr-16
12
Hints on GC Turning
• Enough Heap Size
• The proportion of Heap dedicated to the young generation
– grant plenty of memory to young generation
– may cause excessive old generation collections or pause time
• Specify desired behaviour
– for parallel collector or parallel compacting collector
– -XX:MaxGCPauseMillis=n
– -XX:GCTimeRatio=n
1/(1+n)
• Diagnosing a Garbage Collection problem
– http://java.sun.com/docs/hotspot/gc1.4.2/example.html
CONFIDENTIAL
2-Apr-16
13
GC Log
• -Xloggc:d:\gc.log
• -XX:+PrintGC
• -XX:+PrintGCDetails
• -XX:+PrintGCTimeStamps – add time stamp
3603.329: [GC [PSYoungGen: 244309K->4520K(244800K)] 705465K->472071K(978816K), 0.0274399 secs]
Young Gen: before GC->After GC
All: before GC->after GC
Pause Time
3603.357: [Full GC [PSYoungGen: 4520K->0K(244800K)]
[PSOldGen: 467550K->407305K(734016K)] 472071K->407305K(978816K)
[PSPermGen: 81712K->81712K(171136K)], 1.3827685 secs]
CONFIDENTIAL
2-Apr-16
14
Visual VM
• Get Runtime Heap Dump
• Monitor
time spent compiling Java byte
codes into native code
• Visual GC
TenuringThreshold & MaxTenuringThreshold
also impacts performance
CONFIDENTIAL
2-Apr-16
15
GC Viewer - Post-mortem analysis
CONFIDENTIAL
2-Apr-16
16
Out of Memory - java.lang.OutOfMemoryError
• Java heap space
– Configuration issue: -Xmx
– Memory Leak
– The excessive use of finalizers
• PermGen space
– Too many classes: -XX:MaxPermSize
• Requested array size exceeds VM limit
– Why need a so big array?
• Request <size> bytes for <reason>. Out of swap space?
– Native Memory Leak?
• <reason> <stack trace> (Native method)
– Native Memory Allocation Issue
CONFIDENTIAL
2-Apr-16
17
Memory Leak vs. OOM
• Memory Leak (OOM != Memory Leak)
– An unintentional memory usage.
– Java memory leaks are objects which are not used/needed anymore, but
which are still reachable and therefore are not removed by the Garbage
Collector.
• How Can We Find Leak
– In most of cases, the biggest objects
• Who Caused Leak?
– follow the reference chain from the object to the (GC) roots
• GC Roots
– Objects on the call stack of the current thread (e.g. method parameters and
local variables)
– The thread itself and classes loaded by the system class loader
– custom class loaders are NOT
CONFIDENTIAL
2-Apr-16
18
Eclipse Memory Analysis – Heap Memory Leak
CONFIDENTIAL
2-Apr-16
19
Perm Memory Leak
• Too Many Interned String
– String.intern()
– Constant String will be interned implicitly
– No Enough Info provided by Heap Dump on Interned String
– If Perm Memory increased dynamically, be careful
• Too Many Classes or Class Load Leak
– Enlarge the perm generation
– Avoid duplicated class loader
CONFIDENTIAL
2-Apr-16
20
Eclipse Memory Analyzer – Perm Generation
• Class Loader Explorer: Java Basic => Class Load Explorer
Loaded Class Number
Class Loader Name
Loaded Object Number
CONFIDENTIAL
2-Apr-16
21
List Duplicated Class Loaders
• Java Basic => Duplicated Classes
CONFIDENTIAL
2-Apr-16
22
Out of Swap Space?
• Request <size> bytes for <reason>. Out of swap space?
– Enlarge the swap space
– Systems with 4GB of ram or less require a minimum of 2GB of swap space
– Systems with 4GB to 16GB of ram require a minimum of 4GB of swap
space
– For Unix Family OS, use pmdump or pmap, libumem for Solaris
# An unexpected error has been detected by SAP Java Virtual Machine:
# java.lang.OutOfMemoryError: requested 2048000 bytes for eArray.cpp:80: GrET*.
Out of swap space or heap resource limit exceeded (check with limits or ulimit)?
# Internal Error (\\...\hotspot\src\share\vm\memory\allocation.inline.hpp, 26), pid=6000, tid=4
CONFIDENTIAL
2-Apr-16
23
Native Memory Leak?
• <reason> <stack trace> (Native method)
– Native Memory Allocation Issue
• C heap exhaustion is usually the result of a memory leak in either
– VM itself
– Core library native code
– Third party native code
• Append -verbose:jni option after JVM args.
– Observe the loading and linking actions of jni dynamic libs
• No many choices
– Check JVM bugs
CONFIDENTIAL
2-Apr-16
24
JDK 6 ! – Performance Enhancement
• Runtime Performance Improvement
– Synchronization & Multiple Threads
– Array Copy
– Background compilation (Runtime Code Optimization)
• GC Performance Improvement
– Parallel Compaction Collector
– Concurrent Low Pause Collector
CONFIDENTIAL
2-Apr-16
25
Last but not least
• Let’s use powerful machine
– 64 bit
– 4 - 8 CPU
– 16GB – 64GB Memory
• Unix Family OS
– Why Java Runs on Windows?
– Solaris
– HP-UX
– Unit
– Linux
• However, Hardware cannot help you survive if the codes are
rubbish!
CONFIDENTIAL
2-Apr-16
26
References
• http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html
• http://wiki.eclipse.org/index.php/MemoryAnalyzer
• http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-orhow-to-find-memory-leaks/
• http://java.sun.com/javase/technologies/hotspot/gc/index.jsp
• http://java.sun.com/javase/technologies/hotspot/gc/memorymanagement_whitepa
per.pdf
• http://www.tagtraum.com/gcviewer-vmflags.html#sun.verbose
• http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
• http://java.sun.com/performance/reference/whitepapers/6_performance.html#2
• http://java.sun.com/docs/hotspot/gc1.4.2/example.html
• http://blogs.sun.com/dagastine/entry/java_6_leads_out_of
• http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html
CONFIDENTIAL
2-Apr-16
27