C O N F I D E N T I A L 17-May-15 1 Java Memory Management Overview Justin Chen Mar 16 rd, 2009.

Slides:



Advertisements
Similar presentations
JVM Internals Douglas Q. Hawkins. JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time.
Advertisements

Java Virtual Machine Profiling
Part IV: Memory Management
… an introduction Peter Varsanyi Garbage collector Confidential.
Dynamic Memory Management
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
1 Overview Assignment 5: hints  Garbage collection Assignment 4: solution.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Memory Leak WEBLOGIC SERVER.  Overview of Java Heap  What is a Memory Leak  Symptoms of Memory Leaks  How to troubleshoot  Tools  Best Practices.
NUMA Tuning for Java Server Applications Mustafa M. Tikir.
UPortal Performance Optimization Faizan Ahmed Architect and Engineering Group Enterprise Systems & Services RUTGERS
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
Memory Management 2010.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
G1 TUNING Shubham Modi( ) Ujjwal Kumar Singh(10772) Vaibhav(10780)
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Page 1 © 2001 Hewlett-Packard Company Tools for Measuring System and Application Performance Introduction GlancePlus Introduction Glance Motif Glance Character.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
1 Garbage Collection Danny Angus. Introduction Student loans, amongst other things, run B2B applications implementing government policy in the UK. We.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Memory Leak Overview and Tools. AGENDA  Overview of Java Heap  What is a Memory Leak  Symptoms of Memory Leaks  How to troubleshoot  Tools  Best.
File System. NET+OS 6 File System Architecture Design Goals File System Layer Design Storage Services Layer Design RAM Services Layer Design Flash Services.
CLR: Garbage Collection Inside Out
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
The Java Virtual Machine Mike Brunt.  What is the JVM?  Main JVM Suppliers  ColdFusion and the JVM  Java J2EE – Java EE Servlet Containers  Where.
Bill Au CBS Interactive Troubleshooting Slow or Hung Java Applications.
Bill Au CBS Interactive Troubleshooting Slow or Hung Java Applications.
Lecture 10 : Introduction to Java Virtual Machine
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
© 2013 IBM Corporation Practical Performance Understand and improve the performance of your application San Hong Li – Technical lead of JTC Shanghai 23.
Installing, Configuring And Troubleshooting Coldfusion Mark A Kruger CFG Ryan Stille CF Webtools.
Power your JVM with Effective Garbage Collection Tuning SiliconIndia Java Conference 29 th October, 2010 Vivekanand Jha.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Computer Systems Week 14: Memory Management Amanda Oddie.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Object Oriented Software Development 4. C# data types, objects and references.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
J2SE 1.5 : Memory Heap and Garbage Collector. Objectives You will be able to understand the JVM Memory Heap You will be able to know what the Garbage.
RealTimeSystems Lab Jong-Koo, Lim
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Object Oriented Programming in
Topic: Java Garbage Collection
CSC 331 Object Oriented Programming Using Java
Java 9: The Quest for Very Large Heaps
Runtime Analysis of Hotspot Java Virtual Machine
Java Virtual Machine Complete subject details are available at:
Simulated Pointers.
Pick Diamonds from Garbage
Am I Reading GC Logs Correctly?
Simulated Pointers.
Strategies for automatic memory management
ColdFusion Performance Troubleshooting and Tuning
Adaptive Code Unloading for Resource-Constrained JVMs
Memory Allocation CS 217.
Interpreting Java Program Runtimes
Java Virtual Machine Profiling. Agenda Introduction JVM overview Performance concepts Monitoring Profiling VisualVM demo Tuning Conclusions.
CSE 542: Operating Systems
Presentation transcript:

C O N F I D E N T I A L 17-May-15 1 Java Memory Management Overview Justin Chen Mar 16 rd, 2009

C O N F I D E N T I A L 17-May-15 2 Before Start … Learning HOW is more important than learning WHAT. 知其然,更要知其所以然

C O N F I D E N T I A L 17-May-15 3 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

C O N F I D E N T I A L 17-May-15 4 Size of Java Types & Objects From Java Spec - byte : 8-bit short : 16-bit Int : 32-bit long : 64-bit char : 16 bit unsigned integer float : 32-bit double: 64-bit boolean: 1 bit? Actual Memory Size - (depends on JVM Implementation) Byte : 16 bytes Short : 16 bytes Integer : 16 bytes Long : 16 bytes Character : 16 bytes Float : 16 bytes Double : 16 bytes Boolean : 16 bytes So, how about Object? -new Object() -new new Object() => 8 bytes new Double[] itself => 16 bytes new ArrayList itself => 24 bytes Why? Memory Layout of Boolean [HEADER: 8 bytes] 8 [value: 1 byte ] 9 [padding: 7 bytes] 16

C O N F I D E N T I A L 17-May-15 5 Shallow and Retained Sizes Shallow Size : the amount of allocated memory to store the object itself. new ArrayList () = 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. new ArrayList () = 80 bytes What’s GC root?

C O N F I D E N T I A L 17-May-15 6 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.

C O N F I D E N T I A L 17-May-15 7 Heap Dump The heap dump is a dump of all the live objects and classes. – Example: java_pid460.hprof 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

C O N F I D E N T I A L 17-May-15 8 User Heap JVM Memory Structure Permanent Native All class instances Arrays -Xmx max New/Old heap size -Xms initial New/Old heap size per-class structures: run-time constant pool, field and method data codes for methods and constructors Interned String -XX:MaxPermSize Eden S0 S1 Old (tenured) GC-able Heap? The memory managed by OS New/Young what is interned String?

C O N F I D E N T I A L 17-May-15 9 How GC works? GC of Serial Collection Empty Survivor Spaces Old Eden FromTo New Object

C O N F I D E N T I A L 17-May 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

C O N F I D E N T I A L 17-May 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

C O N F I D E N T I A L 17-May GC Algorithm Performance Metrics Throughput – High is better 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 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

C O N F I D E N T I A L 17-May 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 –

C O N F I D E N T I A L 17-May GC Log -Xloggc:d:\gc.log -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps – add time stamp : [GC [PSYoungGen: K->4520K(244800K)] K->472071K(978816K), secs] Young Gen: before GC->After GCAll: before GC->after GC Pause Time : [Full GC [PSYoungGen: 4520K->0K(244800K)] [PSOldGen: K->407305K(734016K)] K->407305K(978816K) [PSPermGen: 81712K->81712K(171136K)], secs]

C O N F I D E N T I A L 17-May Visual VM Get Runtime Heap Dump Monitor Visual GC time spent compiling Java byte codes into native code TenuringThreshold & MaxTenuringThreshold also impacts performance

C O N F I D E N T I A L 17-May GC Viewer - Post-mortem analysis

C O N F I D E N T I A L 17-May 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 bytes for. Out of swap space? – Native Memory Leak? (Native method) – Native Memory Allocation Issue

C O N F I D E N T I A L 17-May 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

C O N F I D E N T I A L 17-May Eclipse Memory Analysis – Heap Memory Leak

C O N F I D E N T I A L 17-May 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

C O N F I D E N T I A L 17-May Eclipse Memory Analyzer – Perm Generation Class Loader Explorer: Java Basic => Class Load Explorer Class Loader Name Loaded Class Number Loaded Object Number

C O N F I D E N T I A L 17-May List Duplicated Class Loaders Java Basic => Duplicated Classes

C O N F I D E N T I A L 17-May Out of Swap Space? Request bytes for. 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 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=468

C O N F I D E N T I A L 17-May Native Memory Leak? (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

C O N F I D E N T I A L 17-May 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

C O N F I D E N T I A L 17-May Last but not least Let’s use powerful machine – 64 bit – 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! However, Hardware cannot help you survive if the codes are rubbish!

C O N F I D E N T I A L 17-May References how-to-find-memory-leaks/ how-to-find-memory-leaks/ per.pdf per.pdf