7.33M

Java Memory Managment

1.

Java Memory
Management
How it’s work?

2.

Java Memory
Management
Maaike van
Putten
Seбn Kennedy

3.

Table of
contents:
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 with
doing memory
management
manually:
• Dangling pointers
• Memory leaks
• Boilerplate code
• Error-prone

6.

Her majesty,
JVM

7.

Overview of the JVM
components for application
execution

8.

Runtime data area :
• The stack
• The heap
• Metaspace
• The program counter register
• The native method stack

9.

The Stack
Storage for
primitive and
reference types.
One thread –
one stack.
LIFO (Last in – First out)

10.

The Stack
Overview of the
frames in the
stack area for
three threads

11.

The Heap
Storage for Objects.
One for all threads.
String pool storage
here.
Consist two memory
areas : young and old
generation space.

12.

The Heap
Generations
New objects are
allocated in the
Eden.
Old gen – place
for longer-lived
object.

13.

The Heap
Overview of
the connection
between the
stack and the
heap.

14.

MetaSpace
Consist :
• Class files
• Structure and methods of
the class
• Constants
• Annotations
• Optimizations

15.

MetaSpace
MetaSpace
allocation

16.

Garbage Collector

17.

18.

What “garbage” means?
These are objects that we no
longer need.

19.

All objects have
connection
between the stack
and the heap.

20.

All objects have
connection
between the stack
and the heap.

21.

Time for
Garbage
Collector.

22.

Marking object by GC
All reachable object from the
heap are marked.

23.

Stop-the-world
strategy :
+ A guarantee that new objects
will not be created.
- Application is pause.

24.

Reference counting
strategy :
+ No need pause the app.
- Island of isolation.

25.

Island of isolation
example

26.

Sweeping by the garbage
collector :
- Normal sweeping
- Sweeping with compacting
- Sweeping with copy

27.

Normal sweeping:
+ Speed
- Bad memory management

28.

Sweeping with
compacting :
+ 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 Sweep
Garbage 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 GC
Divides 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 GC
Uses reference coloring
Works on 64-bit systems
Fragmentation is avoided by using
relocation.
Use load barriers

37.

Comparing GC

38.

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 Memory
Leaks
3 metrics :
• Heap memory
footprint
• Garbage
collection activity
• Heap dump

40.

Program with
memory leak

41.

Heap memory
footprint (leakmemory code)

42.

Garbage collector
activity (leakmemory code)

43.

Heap dump
(leak-memory
code)

44.

Heap
memory
footprint
(leak-free
code)

45.

GC Activity
(leak-free
code)

46.

Short advise to avoid
memory 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 section
Thank you
for attention!
English     Русский Rules