SlideShare a Scribd company logo
How to memory efficient Code?
Ram Lakshmanan
Architect – GCeasy.io, fastThread.io, HeapHero.io
Confoo 2019
Do I need to care about memory?
It’s very cheap!
1970s
1 Byte = 1 $
2019
FRACTION OF COST
Here is my case: Memory is not cheap!
Memory saturates first.
Other resources are
partially/under utilized.
3. Storage
4. Network
1. CPU
2. Memory
Do you know how much memory applications wastes?
30 – 90%
You can't optimize, what you can't
measure
A famous saying
How to measure wasted memory?
Step 1: Capture Heap dumps in production*
https://blog.heaphero.io/2017/10/13/how-to-capture-java-heap-dumps-7-options/
Step 2: Analyze with HeapHero: https://heaphero.io
Reports amount of memory wasted, code triggering it, recommendations to fix.
* - if possible
Analyze real-world example
String Duplication
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'
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
How intern() works?
“Hello World”
s1
s2
String pool
String s1 = new String("Hello World").intern();
String s2 = new String("Hello World").intern();
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
Solution 3: Use String literals
public static final String HELLO_WORLD = “Hello World”;
Solution 4: Avoid creating strings
In DB store 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
…. 124
Inefficiency in collections
Inefficiency in Collections
List<User> users = new ArrayList<>();
users.add(user);
2 54 6 7 8 1093
wasted
11
ArrayList underlyingly maintains
Object[]
initial capacity = 10
1 2 54 6 7 8 1093
Inefficiency in Collections
wasted
List<User> users = new ArrayList<>();
for (int counter = 1; counter <= 11; ++counter) {
users.add(user);
}
1 2 54 6 7 8 1093 11 12 1514 17 18 201913 16
1 2 54 6 7 8 93 11 12 1514 17 18 201913 16 21 22 2524 26 27 28 2923 31 32 3534 37 38 403933 3630
wasted
10
for (int counter = 1; counter <= 21; ++counter) {
users.add(user);
}
Recommendation 1: use capacity
new ArrayList<>(3);
new ArrayList<>();
Recommendation 2: lazy initialization
private List<User> users = new ArrayList<>();
public void addUser(User user) {
users.add(user);
}
private List<User> users;
public void addUser(User user) {
if (users == null) {
users = new ArrayList<>();
}
users.add(user);
}
Recommendation 3: avoid clear()
List<User> users = new ArrayList<>();
users = null;
List<User> users = new ArrayList<>();
users.clear();
Let’s understand overhead of an
Object
Object overhead
public class User {
private String name;
private int age;
private float weight;
}
new User();
Object Header
name
age
weight
12 bytes
4 bytes
4 bytes
4 bytes
Fixed overhead
1. Virtual method invocation
2. ‘synchronized’ object lock
3. Garbage collection
4. Object book keeping
24 bytes
Boxed numbers: java.lang.Integer
• int: 4 bytes
• java.lang.Integer: 16 bytes
Data is 4 bytes, object overhead is 12 bytes
Yesterday’s key note: 2 instance variables/class
public class User {
private int field1;
private int field2;
private int field3;
:
private int field10;
}
public class User {
private Object1 obj1;
private Object2 obj2;
}
public class Object1 {
private int field1;
private Object3 obj3;
}
public class Object2 {
private int field2;
private Object4 obj4;
}
public class Object3 {
private int field3;
private Object5 obj5;
}
public class Object4 {
private int field4;
private Object6 obj6;
}52 bytes
i.e. 12 bytes header + (10 ints* 4 bytes) public class Object5 {
private int field5;
private Object6 obj7;
}
public class Object6 {
private int field6;
private Object3 obj8;
}
public class Object7 {
private int field7;
private Object4 obj9;
}
public class Object8 {
private int field8;
private int field9;
}
public class Object9 {
private int field10;
}
196 bytes (~4x more needed)
= 10 object headers * 12 + (10 ints * 4 bytes)
+ (9 object references * 4)
How all memory is wasted?
1. Duplicate Strings: https://heaphero.io/heap-recommendations/duplicate-strings.html
2. Wrong memory size settings
3. Inefficient Collections: http://heaphero.io/heap-recommendations/inefficient-collections.html
4. Duplicate Objects: https://heaphero.io/heap-recommendations/duplicate-objects.html
5. Duplicate arrays: https://heaphero.io/heap-recommendations/duplicate-primitive-arrays.html
6. Inefficient arrays: https://heaphero.io/heap-recommendations/inefficient-primitive-arrays.html
7. Objects waiting for finalization: https://heaphero.io/heap-recommendations/objects-waiting-
finalization.html
8. Boxed numbers: https://heaphero.io/heap-recommendations/boxed-numbers.html
9. Object Headers: https://heaphero.io/heap-recommendations/object-headers.html
Reduce computing cost
Better Response time
Reduce Garbage Collection Pause Time
Optimizing memory
Fascinating to read
Thank you!!
ram@tier1app.com
Ram Lakshmanan
@tier1app
https://www.linkedin.com/company/gceasy
Any Questions
Deck will be published in:
http://blog.heaphero.io

More Related Content

What's hot (20)

PPTX
Shooting the troubles: Crashes, Slowdowns, CPU Spikes
Tier1 app
 
PPTX
System Calls
David Evans
 
PPTX
Making a Process
David Evans
 
PPTX
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
PPTX
Crossing into Kernel Space
David Evans
 
PDF
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
 
PDF
Silicon Valley JUG: JVM Mechanics
Azul Systems, Inc.
 
PPTX
The Art of JVM Profiling
Andrei Pangin
 
PPTX
Thread dump troubleshooting
Jerry Chan
 
PPTX
Scheduling
David Evans
 
PDF
Refactoring for testability c++
Dimitrios Platis
 
PPTX
Do we need Unsafe in Java?
Andrei Pangin
 
PPT
jvm goes to big data
srisatish ambati
 
PDF
Cassandra is great but how do I test my application?
Christopher Batey
 
PPTX
Choosing the right parallel compute architecture
corehard_by
 
PDF
Java Heap Dump Analysis Primer
Kyle Hodgson
 
PPTX
Down to Stack Traces, up from Heap Dumps
Andrei Pangin
 
PDF
Counter Wars (JEEConf 2016)
Alexey Fyodorov
 
PDF
Feldo: Function Event Listing and Dynamic Observing for Detecting and Prevent...
Tzung-Bi Shih
 
PDF
marko_go_in_badoo
Marko Kevac
 
Shooting the troubles: Crashes, Slowdowns, CPU Spikes
Tier1 app
 
System Calls
David Evans
 
Making a Process
David Evans
 
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
Crossing into Kernel Space
David Evans
 
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
 
Silicon Valley JUG: JVM Mechanics
Azul Systems, Inc.
 
The Art of JVM Profiling
Andrei Pangin
 
Thread dump troubleshooting
Jerry Chan
 
Scheduling
David Evans
 
Refactoring for testability c++
Dimitrios Platis
 
Do we need Unsafe in Java?
Andrei Pangin
 
jvm goes to big data
srisatish ambati
 
Cassandra is great but how do I test my application?
Christopher Batey
 
Choosing the right parallel compute architecture
corehard_by
 
Java Heap Dump Analysis Primer
Kyle Hodgson
 
Down to Stack Traces, up from Heap Dumps
Andrei Pangin
 
Counter Wars (JEEConf 2016)
Alexey Fyodorov
 
Feldo: Function Event Listing and Dynamic Observing for Detecting and Prevent...
Tzung-Bi Shih
 
marko_go_in_badoo
Marko Kevac
 

Similar to How to write memory efficient code? (20)

PDF
Intro to Python
Daniel Greenfeld
 
PDF
Approximate "Now" is Better Than Accurate "Later"
NUS-ISS
 
PDF
Memory management
Kuban Dzhakipov
 
PPTX
Why learn Internals?
Shaul Rosenzwieg
 
PPTX
Fantastic caches and where to find them
Alexey Tokar
 
PDF
Memory Management In Python The Basics
Nina Zakharenko
 
PDF
Python Machine Learning Cookbook Early Release 1st Ed Chris Albon
tiyhaoxew964
 
PPTX
Optimizing mobile applications - Ian Dundore, Mark Harkness
ozlael ozlael
 
PPTX
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
PDF
Everything You Always Wanted to Know About Memory in Python - But Were Afraid...
Piotr Przymus
 
PDF
High Performance With Java
malduarte
 
PDF
S2-Programming_with_Data_Computational_Physics.pdf
CARLOSANDRESVIDALBET
 
PDF
Pycon2017 instagram keynote
Lisa Guo
 
PDF
Celery: The Distributed Task Queue
Richard Leland
 
PPT
Talking trash
michael.labriola
 
PDF
The Performance Comparison of a Brute-Force Password Cracking Algorithm using...
GiselleginaGloria
 
PDF
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Piotr Przymus
 
PDF
20_Python_Libraries_You_Aren't_Using_But_Should.pdf
omonovasadbek0821
 
Intro to Python
Daniel Greenfeld
 
Approximate "Now" is Better Than Accurate "Later"
NUS-ISS
 
Memory management
Kuban Dzhakipov
 
Why learn Internals?
Shaul Rosenzwieg
 
Fantastic caches and where to find them
Alexey Tokar
 
Memory Management In Python The Basics
Nina Zakharenko
 
Python Machine Learning Cookbook Early Release 1st Ed Chris Albon
tiyhaoxew964
 
Optimizing mobile applications - Ian Dundore, Mark Harkness
ozlael ozlael
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Everything You Always Wanted to Know About Memory in Python - But Were Afraid...
Piotr Przymus
 
High Performance With Java
malduarte
 
S2-Programming_with_Data_Computational_Physics.pdf
CARLOSANDRESVIDALBET
 
Pycon2017 instagram keynote
Lisa Guo
 
Celery: The Distributed Task Queue
Richard Leland
 
Talking trash
michael.labriola
 
The Performance Comparison of a Brute-Force Password Cracking Algorithm using...
GiselleginaGloria
 
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Piotr Przymus
 
20_Python_Libraries_You_Aren't_Using_But_Should.pdf
omonovasadbek0821
 
Ad

More from Tier1 app (20)

PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
PPTX
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
PPTX
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
PPTX
Troubleshooting JVM Outages – 3 Fortune 500 Case Studies
Tier1 app
 
PPTX
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
PPTX
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
PPTX
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
PPTX
Common Memory Leaks in Java and How to Fix Them
Tier1 app
 
PPTX
7 Micro-Metrics That Predict Production Outages in Performance Labs Webinar
Tier1 app
 
PPTX
Top 5 Java Performance Problems Presentation!
Tier1 app
 
PPTX
Mastering Thread Dump Analysis: 9 Tips & Tricks
Tier1 app
 
PPTX
How to Check and Optimize Memory Size for Better Application Performance
Tier1 app
 
PPTX
TroubleshootingJVMOutages-3CaseStudies (1).pptx
Tier1 app
 
PPTX
TroubleshootingJVMOutages-3CaseStudies.pptx
Tier1 app
 
PPTX
Major Outages in Major Enterprises Payara Conference
Tier1 app
 
PPTX
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
PPTX
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
PPTX
Top-5-java-perf-problems-jax_mainz_2024.pptx
Tier1 app
 
PPTX
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
Tier1 app
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Micro-Metrics Every Performance Engineer Should Validate Before Sign-Off
Tier1 app
 
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Troubleshooting JVM Outages – 3 Fortune 500 Case Studies
Tier1 app
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Common Memory Leaks in Java and How to Fix Them
Tier1 app
 
7 Micro-Metrics That Predict Production Outages in Performance Labs Webinar
Tier1 app
 
Top 5 Java Performance Problems Presentation!
Tier1 app
 
Mastering Thread Dump Analysis: 9 Tips & Tricks
Tier1 app
 
How to Check and Optimize Memory Size for Better Application Performance
Tier1 app
 
TroubleshootingJVMOutages-3CaseStudies (1).pptx
Tier1 app
 
TroubleshootingJVMOutages-3CaseStudies.pptx
Tier1 app
 
Major Outages in Major Enterprises Payara Conference
Tier1 app
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Top-5-java-perf-problems-jax_mainz_2024.pptx
Tier1 app
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
Tier1 app
 
Ad

Recently uploaded (20)

PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Horarios de distribución de agua en julio
pegazohn1978
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Introduction presentation of the patentbutler tool
MIPLM
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 

How to write memory efficient code?

  • 1. How to memory efficient Code? Ram Lakshmanan Architect – GCeasy.io, fastThread.io, HeapHero.io Confoo 2019
  • 2. Do I need to care about memory? It’s very cheap! 1970s 1 Byte = 1 $ 2019 FRACTION OF COST
  • 3. Here is my case: Memory is not cheap! Memory saturates first. Other resources are partially/under utilized. 3. Storage 4. Network 1. CPU 2. Memory
  • 4. Do you know how much memory applications wastes? 30 – 90%
  • 5. You can't optimize, what you can't measure A famous saying
  • 6. How to measure wasted memory? Step 1: Capture Heap dumps in production* https://blog.heaphero.io/2017/10/13/how-to-capture-java-heap-dumps-7-options/ Step 2: Analyze with HeapHero: https://heaphero.io Reports amount of memory wasted, code triggering it, recommendations to fix. * - if possible
  • 9. 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'
  • 10. 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
  • 11. How intern() works? “Hello World” s1 s2 String pool String s1 = new String("Hello World").intern(); String s2 = new String("Hello World").intern();
  • 12. 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
  • 13. Solution 3: Use String literals public static final String HELLO_WORLD = “Hello World”;
  • 14. Solution 4: Avoid creating strings In DB store 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 …. 124
  • 16. Inefficiency in Collections List<User> users = new ArrayList<>(); users.add(user); 2 54 6 7 8 1093 wasted 11 ArrayList underlyingly maintains Object[] initial capacity = 10
  • 17. 1 2 54 6 7 8 1093 Inefficiency in Collections wasted List<User> users = new ArrayList<>(); for (int counter = 1; counter <= 11; ++counter) { users.add(user); } 1 2 54 6 7 8 1093 11 12 1514 17 18 201913 16 1 2 54 6 7 8 93 11 12 1514 17 18 201913 16 21 22 2524 26 27 28 2923 31 32 3534 37 38 403933 3630 wasted 10 for (int counter = 1; counter <= 21; ++counter) { users.add(user); }
  • 18. Recommendation 1: use capacity new ArrayList<>(3); new ArrayList<>();
  • 19. Recommendation 2: lazy initialization private List<User> users = new ArrayList<>(); public void addUser(User user) { users.add(user); } private List<User> users; public void addUser(User user) { if (users == null) { users = new ArrayList<>(); } users.add(user); }
  • 20. Recommendation 3: avoid clear() List<User> users = new ArrayList<>(); users = null; List<User> users = new ArrayList<>(); users.clear();
  • 22. Object overhead public class User { private String name; private int age; private float weight; } new User(); Object Header name age weight 12 bytes 4 bytes 4 bytes 4 bytes Fixed overhead 1. Virtual method invocation 2. ‘synchronized’ object lock 3. Garbage collection 4. Object book keeping 24 bytes
  • 23. Boxed numbers: java.lang.Integer • int: 4 bytes • java.lang.Integer: 16 bytes Data is 4 bytes, object overhead is 12 bytes
  • 24. Yesterday’s key note: 2 instance variables/class public class User { private int field1; private int field2; private int field3; : private int field10; } public class User { private Object1 obj1; private Object2 obj2; } public class Object1 { private int field1; private Object3 obj3; } public class Object2 { private int field2; private Object4 obj4; } public class Object3 { private int field3; private Object5 obj5; } public class Object4 { private int field4; private Object6 obj6; }52 bytes i.e. 12 bytes header + (10 ints* 4 bytes) public class Object5 { private int field5; private Object6 obj7; } public class Object6 { private int field6; private Object3 obj8; } public class Object7 { private int field7; private Object4 obj9; } public class Object8 { private int field8; private int field9; } public class Object9 { private int field10; } 196 bytes (~4x more needed) = 10 object headers * 12 + (10 ints * 4 bytes) + (9 object references * 4)
  • 25. How all memory is wasted? 1. Duplicate Strings: https://heaphero.io/heap-recommendations/duplicate-strings.html 2. Wrong memory size settings 3. Inefficient Collections: http://heaphero.io/heap-recommendations/inefficient-collections.html 4. Duplicate Objects: https://heaphero.io/heap-recommendations/duplicate-objects.html 5. Duplicate arrays: https://heaphero.io/heap-recommendations/duplicate-primitive-arrays.html 6. Inefficient arrays: https://heaphero.io/heap-recommendations/inefficient-primitive-arrays.html 7. Objects waiting for finalization: https://heaphero.io/heap-recommendations/objects-waiting- finalization.html 8. Boxed numbers: https://heaphero.io/heap-recommendations/boxed-numbers.html 9. Object Headers: https://heaphero.io/heap-recommendations/object-headers.html
  • 26. Reduce computing cost Better Response time Reduce Garbage Collection Pause Time Optimizing memory
  • 28. Thank you!! [email protected] Ram Lakshmanan @tier1app https://www.linkedin.com/company/gceasy Any Questions Deck will be published in: http://blog.heaphero.io

Editor's Notes

  • #8: https://heaphero.io/my-heap-report.jsp?p=YXJjaGl2ZWQvMjAxOS8wMy8xNC8tLVRQQkUxLWhlYXBkdW1wLTE3XzA4XzE3XzEwXzMwLmJpbi5nei0zLTQ0LTE1Lmpzb24tLQ== https://heaphero.io/my-heap-report.jsp?p=YXJjaGl2ZWQvMjAxOS8wMy8xNC8tLVRQU1AxLWhlYXBkdW1wLTE3XzA4XzE3XzEwXzMwLmJpbi5nei0zLTI1LTI3Lmpzb24tLQ==