2014-08-28 ECS Java Memory Management
2014-08-28 ECS Java Memory Management
WHILST EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION
CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED.
ALL PERFORMANCE DATA INCLUDED IN THIS PRESENTATION HAVE BEEN GATHERED IN A CONTROLLED
ENVIRONMENT. YOUR OWN TEST RESULTS MAY VARY BASED ON HARDWARE, SOFTWARE OR
INFRASTRUCTURE DIFFERENCES.
ALL DATA INCLUDED IN THIS PRESENTATION ARE MEANT TO BE USED ONLY AS A GUIDE.
IBM AND ITS AFFILIATED COMPANIES SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF
THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION.
NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR SHALL HAVE THE EFFECT OF:
- CREATING ANY WARRANT OR REPRESENTATION FROM IBM, ITS AFFILIATED COMPANIES OR ITS OR
THEIR SUPPLIERS AND/OR LICENSORS
2 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Introduction to the speaker
3 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Goals of this talk
4 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Agenda
5 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Understanding Java Memory Management
■ Java runs as a Operating System (OS) level process, with the restrictions that the OS
imposes:
0 GB 2 GB 4 GB
-Xmx
0x40000000 0xC0000000
0x0 0x80000000 0xFFFFFFFF
6 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Java objects with “native” resources
■ Example: java.lang.Thread
7 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Anatomy of a Java Object
■ Question: An int (eg. 10) is 32 bits, but how much bigger is an Integer object?
(for a 32bit platform)
(a) x1
(b) x1.5
(c) x2
(d) x3
8 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Anatomy of a Java Object
public static void main(String[] args) {
Integer myInteger = new Integer(10);
}
■ Additionally, all Objects are 8 byte aligned (16 byte for CompressedOops with large heaps)
9 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Anatomy of a 64bit Java Object
public static void main(String[] args) {
Integer myInteger = new Integer(10);
}
Class pointer
Class pointer
Flags Locks Flags int Locks int, eg. 10 Java Object
Class pointer
Class pointer
Flags Locks Flags Size int Locks Size int, eg. 10 Array Object
10 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Object Field Sizes
11 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Compressed References and CompressedOOPs
12 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Allocating (slightly) more complex objects
■
char char char char char char char char
Class pointer Flags Locks Size
M Y T I G
char[]
■ S R N
■ 128 bits of char data, stored in 480 bits of memory, size ratio of x3.75
– Maximum overhead would be x24 for a single character!
■
13 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Java Collections
■ Each Java Collection has a different level of function, and memory overhead
java.util.HashSet
Increasing Function
Increasing Size
java.util.HashMap
java.util.Hashtable
java.util.LinkedList
java.util.ArrayList
■ Using the wrong type of collection can incur significant additional memory overhead
14 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
HashSet
public static void main(String[] args) {
HashSet myHashSet = new HashSet();
}
15 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
HashMap
public static void main(String[] args) {
HashMap myHashMap = new HashMap();
}
16 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
HashMap$Entry
17 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Hashtable
public static void main(String[] args) {
Hashtable myHashtable = new Hashtable();
}
18 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Hashtable$Entry
19 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
LinkedList
public static void main(String[] args) {
LinkedList myLinkedList = new LinkedList();
}
20 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
LinkedList$Entry / Link
21 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
ArrayList
public static void main(String[] args) {
ArrayList myArrayList = new ArrayList();
}
23 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Collections Summary
24 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Hash* collections vs others
25 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Empty space in collections
26 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Expansion of collections
27 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Collections Summary
28 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Collections Summary
■ 16% of the Java heap used just for the collection objects !!
29 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Analyzing your Collections
30 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Analyzing your Collections
31 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Analyzing your Collections
32 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Analyzing your Collections
33 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Analyzing your Collections
34 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Collection Analysis for PlantsByWebSphere Example
35 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Improvements in the JDK: WeakHashMap
36 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Techniques for minimizing memory
37 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Summary
38 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Questions?
39 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Backup
40 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Process Memory Monitoring
■ GC and Memory Visualiser (GCMV) in ISA can visualize some OS tool output:
– AIX, Linux, Windows, z/OS
-verbose:gc
-Xverbosegclog:[DIR_PATH][FILE_NAME]
-Xverbosegclog:[DIR_PATH][FILE_NAME],X,Y
– where:
[DIR_PATH] is the directory where the file should be written
[FILE_NAME] is the name of the file to write the logging to
X is the number of files to
Y is the number of GC cycles a file should contain
– Performance Cost:
● Very, very small – cost of I/O to stderr/file only
● basic testing shows a <1ms overhead per GC cycle
■ Learn:
– Debugging from Dumps:
● http://www.ibm.com/developerworks/java/library/j-memoryanalyzer/index.html
– Why the Memory Analyzer (with IBM Extensions) isn't just for memory leaks
anymore:
● http://www.ibm.com/developerworks/websphere/techjournal/1103_supauth/1103_supauth.html
■ Discuss:
– IBM on Troubleshooting Java Applications Blog:
● https://www.ibm.com/developerworks/mydeveloperworks/blogs/troubleshootingjava/
– IBM Java Runtimes and SDKs Forum:
● http://www.ibm.com/developerworks/forums/forum.jspa?forumID=367&start=0
52 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation
Copyright and Trademarks
IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International
Business Machines Corp., and registered in many jurisdictions worldwide.
Other product and service names might be trademarks of IBM or other companies.
A current list of IBM trademarks is available on the Web – see the IBM “Copyright and
trademark information” page at URL: www.ibm.com/legal/copytrade.shtml
53 From Java Code to Java Heap: Understanding the Memory Usage of Your Application © 2014 IBM Corporation