Memory efficient programming

23
Reservations Gateway Inc. Reservations Gateway Inc. YOUR LINK to e-TRAVEL SOLUTIONS YOUR LINK to e-TRAVEL SOLUTIONS October 2013 - Efficient Programming- - Efficient Programming- Creating memory efficient Creating memory efficient Applications Applications Any intelligent fool can make things bigger and more complex... Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction It takes a touch of genius - and a lot of courage to move in the opposite direction - - Albert Einstein Albert Einstein Indika Maligaspe
  • date post

    18-Oct-2014
  • Category

    Technology

  • view

    426
  • download

    0

description

A simple way to develop in java with performance in mind. In this presentation we will look at some of the basics that we tend to miss when developing, which eventually leads to memory inefficiencies in our applications.

Transcript of Memory efficient programming

Page 1: Memory efficient programming

Reservations Gateway Inc.Reservations Gateway Inc. YOUR LINK to e-TRAVEL SOLUTIONSYOUR LINK to e-TRAVEL SOLUTIONS

October 2013

- Efficient Programming-- Efficient Programming-

Creating memory efficient Creating memory efficient ApplicationsApplications

““Any intelligent fool can make things bigger and more complex... Any intelligent fool can make things bigger and more complex...

It takes a touch of genius - and a lot of courage to move in the It takes a touch of genius - and a lot of courage to move in the opposite directionopposite direction””

- - Albert EinsteinAlbert Einstein

Indika Maligaspe

Page 2: Memory efficient programming

Intro...Intro...

October 2013

Indika Maligaspe

K. Indika Maligaspe

Developer, Designer, Architect , Trainer

Tech Geek specialized in JAVA and .Net

Over 13 years if experience in IT / Web Technologies

Lover of almost all sports... http://www.linkedin.com/profile/view?id=201732082&trk=nav_responsive_tab_profile

Page 3: Memory efficient programming

October 2013

Understanding the memory modal of JVMUnderstanding the memory modal of JVM

Efficient usage of Java Efficient usage of Java

What's in a CollectionWhat's in a Collection

How to analyzeHow to analyze

QAQA

What we will coverWhat we will cover

Indika Maligaspe

Page 4: Memory efficient programming

October 2013

The JVM is made up of several areasThe JVM is made up of several areas

Threads & StackThreads & Stack

HeapHeap

Non Heap Permanent generation Non Heap Permanent generation

Understanding the memory Understanding the memory modal of JVMmodal of JVM

Indika Maligaspe

Page 5: Memory efficient programming

October 2013

Three main areas that effects performanceThree main areas that effects performance

Young GenerationYoung Generation

Newly Created ObjectsNewly Created Objects

Short lived, smaller footprintsShort lived, smaller footprints

Broken down to 3 areas as Eden & 2 Survivor SpacesBroken down to 3 areas as Eden & 2 Survivor Spaces

Old GenerationOld Generation

Where older objects are maintainedWhere older objects are maintained

GC runs less frequentlyGC runs less frequently

Larger FootprintsLarger Footprints

Understanding the memory Understanding the memory modal of JVMmodal of JVM

Indika Maligaspe

Page 6: Memory efficient programming

October 2013

Three main areas that effects performance cntd.Three main areas that effects performance cntd.

Permanent GenerationPermanent Generation

Meta data about the objects in the heapMeta data about the objects in the heap

Information about classes and methodsInformation about classes and methods

Understanding the memory Understanding the memory modal of JVMmodal of JVM

Indika Maligaspe

Page 7: Memory efficient programming

October 2013

Understanding the memory Understanding the memory modal of JVMmodal of JVM

Indika Maligaspe

Page 8: Memory efficient programming

October 2013

Anatomy of a Java Object - IntegerAnatomy of a Java Object - Integer

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

public static void main(String[] args){int integer = 10;

}

Memory allocation of an int = 32bits Memory allocation of an Integer Object = 128bits (4:1)

public static void main(String[] args){Integer integer = new

Integer(10);}

Page 9: Memory efficient programming

October 2013

Anatomy of a Java ObjectAnatomy of a Java Object

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

public static void main(String[] args){int integer = 10;

}public static void main(String[] args){

Integer integer = new Integer(10);}

Page 10: Memory efficient programming

October 2013

Anatomy of a Java ObjectAnatomy of a Java Object

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

Page 11: Memory efficient programming

October 2013

Anatomy of a Java Object - StringsAnatomy of a Java Object - Strings

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

public static void main(String[] args){String myString = new

String(“MyString”);}

128 bits of char data, stored in 480 bits of memory Maximum overhead is char * 24

Page 12: Memory efficient programming

October 2013

Typical inefficient codeTypical inefficient code

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

public static void main(String[] args){

1. String[] constVals = {“A”,”B”,”C”,”D”};2. String valToCheck = “D”;3. Integer indexToCheck = new Integer(0);

While(loop *10){4. String toSave = new String();

SaveData(....,...,....,toSave);}

While(loop *10){5. Integer bookingData = new Integer(rs.getInt(1));6. Integer retries = new Integer(rs.getString(2));

}}

Page 13: Memory efficient programming

October 2013

Another String ProblemAnother String Problem

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

public static void main(String[] args){1. String s1 = “Hello”;2. String s2 = “World”;3. String s3 = s1 +s2

}

The above is equal to -String s3 = new StringBuilder(String.valueOf(s1)).append(s2).toString();

Avoid String appending Use StringBuffers

Page 14: Memory efficient programming

October 2013

Efficient usage of Java - Summary Efficient usage of Java - Summary

Efficient usage of JavaEfficient usage of Java

Indika Maligaspe

Objects are much larger the the data you store in them.

Use PDT's where ever possible Avoid using new instances and re-use existing instances

as mush as possible Avoid String operations, at least unless you must Minimize objects that are long lasting and not used they

will be moved to old generation and a GC in old gen is

stop the world

Page 15: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

Each Java collection does different functionality and they have different memory usages

java.util.HashSet

java.util.HashMap

java.util.Hashtable

java.util.LinkedList

java.util.ArrayList

Page 16: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

HashMap - java.util.HashMap

Implementation - “An Object that maps keys and values . A map cannot contain duplicate keys, each key can map to at most one value

Implementation is an array of HashMap$Entry Objects Default capacity is 16 entries Empty size is 128 bytes Overhead is 48 bytes for HasMap, plus (16 + (entries* 4 bytes)) for an array and

the overhead of HashMap$Entry objects

Page 17: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

LinkedList – java.util.ArrayList

Implementation - “An ordered collection (AKA sequence). The user of this interface has precise control over where in the list each element is inserted.

Implementation is an array of Objects Default capacity is 10 entries Empty size is 88 bytes Overhead is 32 bytes for ArrayList, plus (16 + (entries* 4 bytes)) for an array For a 10,000 entry ArrayList, the overhead is - 40k

Page 18: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

HashMap$Entry

Each HashMap$Entry contains

Int KeyHash Object next Object key Object value

Additional 32 bytes per key <> value entry Overhead of a HashMap is therefore – 48 bytes, plus 36 bytes per entry For a 10,000 entry HashMap, the overhead is - 360k

Page 19: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

Summary of Collections

Collection Default Capacity Empty Size 10k Overhead

HashSet 16 144 360k

HashMap 16 128 360k

Hashtable 11 104 360k

LinkedList 1 48 240k

ArrayList 10 88 40k

StringBuffer 16 72 240k

Page 20: Memory efficient programming

October 2013

Indika Maligaspe

What's in a CollectionWhat's in a Collection

Hash* collections vs Others

Hash collections are much larger (almost 9x of an ArrayList)

Additional size helps search / add / remove (performance is constant to Hash

collections but linear to ArrayList) Really know when to use what and use properly

Page 21: Memory efficient programming

October 2013

Indika Maligaspe

How to analyzeHow to analyze

Several tools available to analyze performance within the JVM

VisualVM - Production / Development

Eclipse – Memory Analysis Tool (MAT) - Development

Jconsole - Development

Just reading hprof - Production / Development

Nagios – Production

NewRelic - Production

Page 22: Memory efficient programming

October 2013

Indika Maligaspe

How to analyzeHow to analyze

Most of the tools will give

Memory Usage (Heap / Perm etc..)

CPU stats for JVM GC usage and behavior Threads and how threads are being used Hot spots

Page 23: Memory efficient programming

October 2013

Indika Maligaspe

Thank You

Reservations Gateway Inc.Reservations Gateway Inc. YOUR LINK to e-TRAVEL SOLUTIONSYOUR LINK to e-TRAVEL SOLUTIONS

Reservations Gateway Inc.Reservations Gateway Inc.

11654 Plaza America Drive , Unit 645 Reston, Virginia 20190-4700

USA

703 286 5331703 433 0146 [email protected] www.rezgateway.com

Tel : Fax :

Email :Web :