Similar presentations:
Java Memory Managment
1.
Java MemoryManagement
How it’s work?
2.
Java MemoryManagement
Maaike van
Putten
Seбn Kennedy
3.
Table ofcontents:
1. Memory management before Java
2. JVM and Java Memory
3. Stack, Heap and MetaSpace
4. Garbage Collector
5. Avoiding Memory Leaks
6. QA section
4.
A long time ago in a galaxy far,far away….
Memory management
in C and C++
5.
Problems withdoing memory
management
manually:
• Dangling pointers
• Memory leaks
• Boilerplate code
• Error-prone
6.
Her majesty,JVM
7.
Overview of the JVMcomponents for application
execution
8.
Runtime data area :• The stack
• The heap
• Metaspace
• The program counter register
• The native method stack
9.
The StackStorage for
primitive and
reference types.
One thread –
one stack.
LIFO (Last in – First out)
10.
The StackOverview of the
frames in the
stack area for
three threads
11.
The HeapStorage for Objects.
One for all threads.
String pool storage
here.
Consist two memory
areas : young and old
generation space.
12.
The HeapGenerations
New objects are
allocated in the
Eden.
Old gen – place
for longer-lived
object.
13.
The HeapOverview of
the connection
between the
stack and the
heap.
14.
MetaSpaceConsist :
• Class files
• Structure and methods of
the class
• Constants
• Annotations
• Optimizations
15.
MetaSpaceMetaSpace
allocation
16.
Garbage Collector17.
18.
What “garbage” means?These are objects that we no
longer need.
19.
All objects haveconnection
between the stack
and the heap.
20.
All objects haveconnection
between the stack
and the heap.
21.
Time forGarbage
Collector.
22.
Marking object by GCAll reachable object from the
heap are marked.
23.
Stop-the-worldstrategy :
+ A guarantee that new objects
will not be created.
- Application is pause.
24.
Reference countingstrategy :
+ No need pause the app.
- Island of isolation.
25.
Island of isolationexample
26.
Sweeping by the garbagecollector :
- Normal sweeping
- Sweeping with compacting
- Sweeping with copy
27.
Normal sweeping:+ Speed
- Bad memory management
28.
Sweeping withcompacting :
+ Good memory
management
- Cost a lot
29.
Sweeping with copy :+ Faster then compacting
- Need more memory space for
copying
30.
Performance GC:• Throughput
• Predictability
• Footprint
31.
Type of GC :• Serial GC
• Parallel GC
• CMS (concurrent mark sweep) GC
• G1 GC
• ZGC (Z garbage collector)
• Shenandoah GC (custom)
32.
Serial GC :Runs on a single thread
Use STW strategy
Mark and copy for young g
Mark and sweep for old g
33.
Parallel GC :Default java 8 GC
Uses multiple threads
Use STW strategy
Mark and copy for young g
Mark and sweep for old g
34.
Concurrent Mark SweepGarbage Collector
Has an improved mark-and-sweep algorithm.
Uses multiple threads
Mark and copy with SWT for young g
Two short pauses in old g GC cycle
Concurrent mark and sweep for old g
35.
G1 GCDivides the heap into smaller regions
Mark and sweep this region
Track of the amount of reachable and unreachable
objects per memory region
Region with most unreachable objects
collected first
36.
Z GCUses reference coloring
Works on 64-bit systems
Fragmentation is avoided by using
relocation.
Use load barriers
37.
Comparing GC38.
Monitoring GC• Allocation rate: How fast the application
allocates objects in memory.
• Heap population: The number of objects
and their size living on the heap.
• Mutation rate: How often references are
updated in memory.
• Average object live time: The time the
objects live on average.
39.
Avoiding MemoryLeaks
3 metrics :
• Heap memory
footprint
• Garbage
collection activity
• Heap dump
40.
Program withmemory leak
41.
Heap memoryfootprint (leakmemory code)
42.
Garbage collectoractivity (leakmemory code)
43.
Heap dump(leak-memory
code)
44.
Heapmemory
footprint
(leak-free
code)
45.
GC Activity(leak-free
code)
46.
Short advise to avoidmemory leaks :
- Use try-with-resources for auto closing
- Avoiding unnecessary String objects using
StringBuilder
- Managing memory usage by using
primitives instead of wrapper classes
- Not use static collections
47.
QA sectionThank you
for attention!