Common Memory Leaks in Java and How to Fix Them

MaliniV3 153 views 27 slides Apr 16, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

Memory leaks in Java can lead to increased resource consumption, sluggish performance, and even system crashes if left unchecked. Identifying and fixing these leaks is crucial for ensuring stable and efficient applications.

Using this deck, our experts will explore the most common causes of memory ...


Slide Content

1

2 Forever Growing Object Pattern-1

3 Same Object Type Multiplying Pattern-2

4 Demo OutOfMemoryError

5 Not So Common Memory Leaks Thread Leaks Uncleared Thread Local Time Taking Finalizers Growing MetaData Objects Collection Objects with Unused References Listeners & Calls Not De-Registered

6 Memory Leak Symptoms 4 Symptoms

7 Memory Behavior of a Healthy Application - Full Garbage Collection Event

8 1. Gradual Memory Increase

9 2. CPU Spike

10 3. Degraded Response Time/Timeouts

11 4. OutOfMemoryError

12 How to Fix Memory Leak? 3-Step Process

13 1. Review OutOfMemoryError Stack Trace Review Stack Trace

14 2. Study GC Behaviour Recommended

15 How to study GC Behavior?  -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:<file-path> Till Java 8 -Xlog:gc*:file=<file-path> From Java 9 Enable GC logs (always) Almost zero overhead

16 GCeasy GC Log Analysis Demo

17 3. Heap Dump Analysis Most Important

18 How to capture Heap Dumps? 8 options: https://blog.heaphero.io/2017/10/13/how-to-capture-java-heap-dumps-7-options/

1. GC Log 10. netstat 12. vmstat 2. Thread Dump 9. dmesg 3. Heap Dump 6. ps 8. Disk Usage 5. top 13. iostat 11. ping 14. Kernel Params 15. App Logs 16. metadata 4. Heap Substitute 7. top -H 19 Open-source script: https://github.com/ycrash/yc-data-script 360° Troubleshooting artifacts ./ yc –p <PROCESS_ID>

20 HeapHero How to analyze Heap Dump?

21 Predict Memory Leaks in Performance Labs 2 Micro-Metrics https://blog.ycrash.io/9-micro-metrics-that-forecast-production-outages-in-performance-labs/

22 Application experiencing OutOfMemoryError in Production

23 Same Application Suffers Acute Memory Leak in Performance Lab, But We Don’t Notice It

24 2. Object Creation Rate Say, Previous Release: 100 mb/sec. New Release: 200 mb/sec 2x more objects to service the same workload. Indicates inefficient code has been committed in the new release It will manifest as high CPU consumption and degraded response time in the production. 

25 Object Creation Rate

26

Thank you, my friends! Ram Lakshmanan @ycrash_rca This deck will be published in: https://blog.ycrash.io https://www.linkedin.com/company/ycrash