4. Do I need to care about memory?
It’s very cheap!
1970s
1 Byte = 1 $
2019
FRACTION OF COST
5. My case: Memory is not cheap!
Memory gets saturated first.
Other resources are partially or under
utilized.
3. Storage
4. Network
1. CPU
2. Memory
6. Memory wasted by my application
1: Capture Heap dumps in production*
https://blog.heaphero.io/2017/10/13/how-to-capture-java-heap-dumps-7-options/
2: Analyze with HeapHero: https://heaphero.io
Reports amount of memory wasted, code triggering it, recommendations to fix.
* - if possible
How to find it?
14. What is duplicate String?
String s1 = new String("Hello World");
String s2 = new String("Hello World");
System.out.println(s1.equals(s2)); // prints 'true'
System.out.println((s1 == s2)); // prints 'false'
15. Solution 1: intern()
String s1 = new String("Hello World").intern();
String s2 = new String("Hello World").intern();
System.out.println(s1.equals(s2)); // prints 'true'
System.out.println((s1 == s2)); // prints 'true'
>>> s1 = intern(’Hello World')
Java
Python
16. How intern() works?
“Hello World”
s1
s2
String pool
String s1 = new String("Hello World").intern();
String s2 = new String("Hello World").intern();
17. Solution 2: UseStringDeduplication
• -XX:+UseStringDeduplication
• Works only with
G1 GC algorithm
Java 8 update 20
• Real life case study:
https://blog.gceasy.io/2018/07/17/disappointing-story-on-memory-optimization/
Long lived objects (-XX:StringDeduplicationAgeThreshold=3)
• Check GC pause time impact
18. Solution 3: Use String Literals
public static final String MY_STRING = “Hello World”;
19. Solution 4: Avoid creating strings
a. Use enum
b. In DB, consider storing data as primitive types!
user
id
Name …. country
1 Joe Libson …. Canada
2 Nathan Ray …. Canada
3 Chris Mang …. USA
4 Erik Pilz …. USA
5 Lakshmi
Singh
…. India
user
id
Name …. country
1 Joe Libson …. 2
2 Nathan Ray …. 2
3 Chris Mang …. 1
4 Erik Pilz …. 1
5 Lakshmi
Singh
…. 145
24. File Descriptors
File descriptor is a handle to access: File, Pipe,
Network Connections. If count grows it’s a lead
indicator that application isn’t closing resources
properly.
Few more…
TCP/IP States, Hosts count, IOPS, ..
Thread States
If BLOCKED thread state count grows, it’s
an early indication that your application has
potential to become unresponsive
GC Throughput
Amount time application spends in processing
customer transactions vs amount of time application
spend in doing GC
Avg allocation rate
Amount of objects created in a unit of
time
GC Latency
If pause time starts to increase, then
it’s an indication that app is suffering
from memory problems
Micrometrics
https://blog.gceasy.io/2019/03/13/micrometrics-to-forecast-application-performance/
25. CI/CD – catch early in the game
Integrate micrometrics into CI/CD pipeline
Avg allocation rate
https://blog.gceasy.io/2016/06/18/garbage-collection-log-analysis-api/
Memory wasted metrics
https://blog.heaphero.io/2018/06/22/heap-dump-analysis-api/
26. Benefits: Optimizing Memory
Save millions, billions of $ in computing cost
Delight customer:
• Better Response time
• Garbage collection pause time drops
28. Thank You my Friends!
Ram Lakshmanan
ram@tier1app.com
@tier1app
linkedin.com/company/gceasy
This deck will be published in: https://blog.heaphero.io