Have you ever faced OutOfMemoryError? Was it in your CI or during the local run? Maybe you faced it as a user while playing your favorite video game or using online banking to pay your bills.
That’s annoying, isn’t it? Every time we give the application more and more memory, it seems never enough…
In Sonar we know how important it is to use memory efficiently. As our tools are often a part of your ci jobs, we care how much you spend on the infrastructure and try to minimize the cost.
So in this session, I will try to share some tips for investigating, fixing, and preventing potential memory leaks and reducing the memory footprint. In simple words, I will show how you can help your applications eat memory « healthier ».
19. Body
● Clothes too small
● Fat % too high
● Difficult to
run/walk/breathe
● BMI too high
● Always hungry
● Not satisfied with the
appearance…
● OutOfMemoryError
● More memory doesn’t help
● Much time on GC (GC
pauses)
● Spending too much CPU
● …
Application
20. Is there any problem?
How to make your app “fit”?
Let’s measure
24. Here are the heapdump, logs
and some memory analysis to
help you investigate…
Aleksey Shipilëv
25. java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.google.protobuf.Utf8$UnsafeProcessor.decodeUtf8(na:2669)
at com.google.protobuf.Utf8.decodeUtf8(na:2905)
…
at com.sonar.A.D.A(na:648)
at com.sonar.A.D.B(na:2257)
at com.sonar.A.D.A(na:3069)
at com.sonar.cpp.F.readUcfg(na:2348)
26. java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.google.protobuf.Utf8$UnsafeProcessor.decodeUtf8(na:2669)
at com.google.protobuf.Utf8.decodeUtf8(na:2905)
…
at com.sonar.A.D.A(na:648)
at com.sonar.A.D.B(na:2257)
at com.sonar.A.D.A(na:3069)
at com.sonar.cpp.F.readUcfg(na:2348)
27.
28. java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.google.protobuf.Utf8$UnsafeProcessor.decodeUtf8(na:2669)
at com.google.protobuf.Utf8.decodeUtf8(na:2905)
…
at com.sonar.A.D.A(na:648)
at com.sonar.A.D.B(na:2257)
at com.sonar.A.D.A(na:3069)
at com.sonar.cpp.F.readUcfg(na:2348)
30. Is there any problem?
How to make your app “fit”?
Let’s measure
Let’s analyze
31.
32. When static fields are eligible for garbage collection?
A) When the last instance is collected
B) When Class<.> instance is collected
C) When JVM exits
D) When class is unloaded
E) None of the above
32
33. When static fields are eligible for garbage collection?
A) When the last instance is collected
B) When Class<.> instance is collected
C) When JVM exits
D) When class is unloaded
E) None of the above
33
38. When static fields are eligible for garbage collection?
A) When the last instance is collected
B) When Class<.> instance is collected
C) When JVM exits
D) When class is unloaded
E) None of the above
F) When classloader is collected
38
39. Static is like sugar
● Static members live almost “forever”
● Static members should be avoided
● Static members shouldn’t be mutable
● Static members shouldn’t be updated from non-static methods
44. Is there any problem?
How to make your app “fit”?
Let’s measure
Let’s analyze
Find the root cause
45.
46. equals() / hashCode() - carbs
● Should be used properly
● Always override equals() and hashCode() together
● Contract: if equals() returns true, hashCode() must be the same
● HashMap keys must be immutable
63. Memory leak
A memory leak is an
unintentional form of
memory consumption
whereby the developer fails
to free an allocated block of
memory when no longer
needed.
- OWASP
69. Reducing memory footprint
● Remove references manually
● Custom classloaders
● Minimize the living scope
● Do all the necessary cleanup
70. Reducing memory footprint
● Remove references manually
● Custom classloaders
● Minimize the living scope
● Do all the necessary cleanup
● Use WeakReference
82. @Test
public void myTest() {
var big = new BigObject().new SmallObject();
Assert.assertTrue(
GraphLayout.parseInstance(big).totalCount() < MAX_SIZE);
}
83. A healthy diet for your Java application
● Avoid statics and mutability
● Clean ThreadLocals
● Free resources
● Avoid Non-static inner classes
● If still need them, be careful
● Reduce the scope of references
● Monitor memory usage