From Java Code To Java Heap: Understanding The Memory Usage of Your Application
From Java Code To Java Heap: Understanding The Memory Usage of Your Application
Important Disclaimers
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
My contact information:
[email protected]
http://www.linkedin.com/profile/view?id=3100666
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Agenda
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Java runs as a Operating System (OS) level process, with the restrictions that the OS
imposes:
0 GB
2 GB
0x40000000
0x80000000
-Xmx
4 GB
Java Heap(s)
0xC0000000
0xFFFFFFFF
Question:
(a) 1 : 1
(b) 1.5 : 1
(c) 2 : 1
(d) 3: 1
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Question:
(a) 1 : 1
(b) 1.5 : 1
(c) 2 : 1
(d) 3: 1
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Field Type
32bit Process
Object
Array
64bit Process
Object
Array
boolean
32
32
byte
32
32
char
32
16
32
16
short
32
16
32
16
int
32
32
32
32
float
32
32
32
32
long
64
64
64
64
double
64
64
64
64
Objects
32
32
64*
64
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Size/Bits
0
10
32
64
96
128
Class pointer
Flags
Locks
int
Class pointer
Flags
Locks
Size
160
192
224
256
288
320
Java Object
int
Array Object
Additionally, all Objects are 8 byte aligned (16 byte for CompressedOops with large heaps)
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Size/Bits
0
11
32
64
96
128
Class pointer
Class pointer
Flags
Locks Flags
int
Class pointer
Class pointer
Flags
Locks Flags
Size
160
Locks
int
192
224
256
288
Java Object
int
Locks
320
Size
int
Array Object
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Using compressed technologies does not remove native heap usage increase
12
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
13
32
64
96
128
Class pointer
Flags
Locks
hash
Class pointer
Flags
Locks
Size
160
count
char
char
192
offset
char
char
224
256
char
320
java.lang.String
value
char
288
char
char
char[]
128 bits of char data, stored in 480 bits of memory, size ratio of 3.75 : 1
Maximum overhead would be 24:1 for a single character!
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Java Collections
Each Java Collection has a different level of function, and memory overhead
HashSet
HashMap
Hashtable
LinkedList
Increasing Size
Increasing Function
ArrayList
14
Using the wrong type of collection can incur significant additional memory overhead
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
HashSet
15
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
HashMap
Overhead is 48 bytes for HashMap, plus (16 + (entries * 4bytes)) for array
Plus overhead of HashMap$Entry objects
16
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
HashMap$Entry
17
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Hashtable
Overhead is 40 bytes for Hashtable, plus (16 + (entries * 4bytes)) for array
Plus overhead of Hashtable$Entry objects
18
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Hashtable$Entry
19
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
LinkedList
20
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
LinkedList$Entry / Link
21
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
ArrayList
Overhead is 32bytes for ArrayList, plus (16 + (entries * 4bytes)) for array
22
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
23
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Default collection size may not be appropriate for the amount of data being held
24
32
64
96
Class
Flags
Locks
Class
Flags
Locks
128
count
160
192
224
256
288
320
352
384
416
448
480
512
544
576
608
640
java.lang.StringBuffer
value
Size char
Y
M charcharcharcharcharcharchar
S T R I N charcharcharcharcharcharcharchar
G
char[]
Expansion of collections
25
32
64
96
Class
Flags
Locks
Class
Flags
Locks
128
count
160
192
224
256
288
320
352
384
416
448
480
512
544
576
608
640
java.lang.StringBuffer
value
Size char
Y
M charcharcharcharcharcharchar
G
charcharcharcharcharcharcharchar
S T R I N charcharcharcharcharcharcharcharcharcharcharcharcharcharchar
T E X T
O F
char[]
Collections Summary
26
Collection
Default
Capacity
Empty Size
10K Entry
Overhead
Expansion
Algorithm
HashSet
16
144
360K
double size
HashMap
16
128
360K
double size
Hashtable
11
104
360K
double size + 1
LinkedList
48
240K
single entries
ArrayList
10
88
40K
size + 50%
StringBuffer
16
72
24
double size
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Collections Summary
16% of the Java heap used just for the collection objects !!
27
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
28
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
29
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
Summary
30
31
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
References
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_sup
auth.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
32
From Java Code to Java Heap: Understanding the Memory Usage of Your Application
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
33
From Java Code to Java Heap: Understanding the Memory Usage of Your Application