Ram Lakshmanan, the founder and the architect of yCrash talked about how to diagnose and solve issues when a app crashes due to a memory leak, thread leak, CPU spike, unresponsiveness, BLOCKED threads, Deadlocks, and Heavy I/O activity.
2. Young old metaspace others
-Xmx -XX:MaxMetaspaceSize
Young: Newly created Objects
Old: New objects that survived one or more minor
GC promoted here
Metaspace: Classes, Methods, metadata
Others Description
Thread Stacks Each thread has a separate memory space.
Controlled by -Xss
Garbage
Collection
Threads, Memory to store GC info
Code Generation Converting bytecode to native code
Socket Buffers TCP Connections (Receive buffer ~37k, Send
Buffer ~2.5k)
JNI JNI program also allocate memory
Can Java process memory consumption
go beyond –Xmx?
3. main( )
a( )
b( )
c( )
x=1
y*
z=2.11
public class SimpleExample {
public static void main(String args[]) {
a();
}
public static void a() {
int x = 1;
b();
}
public static void b() {
Car y = new Car();
c();
}
public static void c() {
float z = 2.11f;
}
}
Thread’s stack
Young old metaspace
car
-Xss
car
metada
How java program is executed?
5. HOW TO SIMULATE?
Source code: https://github.com/ycrash/buggyapp
public class StackOverflowDemo {
public void start() {
start( );
}
}
Thread’s stack
frame
Infinite times start( )
is present in the
stack
start( )
start( )
start( )
start( )
start( )
start( )
7. HOW TO SIMULATE?
Open Source: https://github.com/ycrash/buggyapp
public class CPUSpikerThread extends Thread
{
@Override
public void run( ) {
while (true) {
}
}
}
9. HOW TO SIMULATE?
Open Source: https://github.com/ycrash/buggyapp
public class OOMDemo {
static HashMap<Object, Object> myMap = new HashMap<>();
public static void start() throws Exception {
long counter = 0;
while (true) {
myMap.put("key" + counter,
"Large stringgggggggggggggggggggggggggggggggggggg“ + counter);
++counter;
}
}
}
10. Hashmap in memory
Key0 Large stringggggggggggggggggggggggggggggggggggg0
Key1 Large stringggggggggggggggggggggggggggggggggggg1
Key2 Large stringggggggggggggggggggggggggggggggggggg2
Key3 Large stringggggggggggggggggggggggggggggggggggg3
: :
: :
: :
: :
: :
14. HOW TO SIMULATE?
public class ThreadLeakDemo {
public static void start() {
while (true) {
new ForeverThread().start();
}
}
}
public class ForeverThread extends Thread {
@Override
public void run() {
while (true) { // Put the thread to sleep forever, so they don't die.
try {
Thread.sleep(10 * 60 * 1000); // Sleeping for 10 minutes repeatedly
} catch (Exception e) {}
}
}
}
Source code: https://github.com/ycrash/buggyapp
15. Oom: unable to create new native thread
Java Heap + metaspace
Physical memory
Threads
Key: Threads are created outside heap,
metaspace
Java Heap + metaspace
Physical memory
Process
-1
Process
-2
Solution:
1. Fix thread leak
2. Increase the Thread Limits Set at
Operating System(ulimit –u)
3. Reduce Java Heap Size
4. Kills other processes
5. Increase physical memory size
6. Reduce thread stack size (-Xss).
Note: can cause StackOverflowError
16. 360° DATA (16 artifacts)
open-source script: https://github.com/ycrash/yc-data-script
18. Thank you friends
Ram Lakshmanan
ram@tier1app.com
@yCrash
https://www.linkedin.com/company/gceasy
This deck will be published in: https://blog.ycrash.io
CREDITS: This presentation template was created by Slidesgo, including
icons by Flaticon, and infographics & images by Freepik