Java Application Performance and Memory Management - vidyasekaran/current_learning GitHub Wiki

Stringbuffer vs string

https://www.infoworld.com/article/2076072/stringbuffer-versus-string.html

Include Static code analysis for code review

  • PMD - STS - MarketPlace - install pmd

How to Write Memory-Efficient Java Code (Good one)

https://www.youtube.com/watch?v=f2aNWtt0QRo

Integer myInteger = new Integer(10);

An int value is 32 bits. How big is an Integer object? 128 bits (4 times of 32 Integer size)

  • in 32 bit machines java object storage is less
  • in 64 bit machine it is twice the size of 32 bit machine memory so we can use compressed Refs/OOPs

https://www.baeldung.com/jvm-compressed-oops

Beyond 32 GB

It's also possible to use compressed pointers when Java heap sizes are greater than 32GB. Although the default object alignment is 8 bytes, this value is configurable using the -XX:ObjectAlignmentInBytes tuning flag. The specified value should be a power of two and must be within the range of 8 and 256.

We can calculate the maximum possible heap size with compressed pointers as follows:

4 GB * ObjectAlignmentInBytes For example, when the object alignment is 16 bytes, we can use up to 64 GB of heap space with compressed pointers.

Please note that as the alignment value increases, the unused space between objects might also increase. As a result, we may not realize any benefits from using compressed pointers with large Java heap sizes.

Futuristic GCs


ZGC, a new addition in Java 11, was an experimental and scalable low-latency garbage collector.

It can handle different ranges of heap sizes while keeping the GC pauses under 10 milliseconds. Since ZGC needs to use 64-bit colored pointers, it does not support compressed references. So, using an ultra-low latency GC like ZGC has to be weighed against using more memory.


String myString = new String("MyString"); -- takes 480 bits of memory and allocates 2 objects for 1 string like below

0 32 64 96 128 160 192 224 Class pointer Flags Locks hash count offset value Class Pointer Flags Locks Size M Y S t r i n g

10 Tips To Improve Your Java Code Performance

https://www.youtube.com/watch?v=frhNwZo_eQE https://www.tutorialdocs.com/article/java-cpu-soar.html

  1. Use String ref ="Java" (reuse if Java String exists in heap; over String ref = new String("Java") - every time it creates a new object.

  2. Use text1.length() ==0 (for less CPU cycles) over text1.equals("").

  3. Avoid Boxing unboxing instead use primitive alone. Primitive types declared locally will be on the stack while primitive types that are defined as part of an object instance are stored on the heap.

  4. Try minimize number of lines of code.

  5. Avoid - for(int x=0; x<itemList.size(); x++) --> think how many times this itemList.size to be called?

int size=itemList.size() instead for(int x=0; x<itemList.size(); x++)

  1. Use java try with resources

  2. Use Streams..

10,000 Java performance tips over 15 years - what did I learn? by Jack Shirazi

https://www.youtube.com/watch?v=OYpTn0nWKR4

Tools to run figure out any issue example: Network,Algorithm,

https://shipilev.net/talks/devoxx-Nov2012-perfMethodology-mindmap.pdf

Most common non manual - Error problem to cause downtime

  • Resource Leaks

    Memory Leaks Closeable: Forgetting to "close()" resources JDBC Connections File handles Disk Space filled

Top Common Problems with tools to diagonize

Slow database queries - JDBC monitoring (eg p6spy), tune SQL.

Inefficient application code - Execution profile (eg jvisualvm), tune code

Too many db queries - JDBC monitoring (eg p6spy),refactor calls

Concurrency issues - stack trace analysis

Memory Leaks - Heap dump analyzer (eg eclipse MAT), identify the objects retaining memory and

fix the code to stop that

Configuration issues (pooling thresholds, request throttling) - keep record of changes,

check diffs, reconcile against test configs

Slow DB - JDBC monitoring (eg p6spy), find clusters of slow queries, correlate against other

events and DB stats

GC Pauses - GC Logging and gc logs analyzer (eg GCViewer), then tune the GC

Memory churn - look for object allocation in a memory profiler (eg jvisualvm)

50 Efficient Code Samples for Java Programming

https://www.alibabacloud.com/blog/50-efficient-code-samples-for-java-programming_596122

This is based on Java app perf and memory mgmt from udemy - Matt Greencroft

Java 8 and Java 11 is dealt

Oracle or OpenJDK fine - hot spot vm used - there are different vms available

https://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do#:~:text=A%20Just%2DIn%2DTime%20(JIT)%20compiler%20is%20a,invoke%20this%20object%20code%20instead.

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead

Run Java program with -XX:+PrintCompilation as argument

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1\bin>java -XX:+PrintCompilation Main 10 59 31 n 0 jdk.internal.misc.Unsafe::getReferenceVolatile (native) 59 27 3 java.util.Objects::equals (23 bytes) 60 33 ! 3 java.util.concurrent.ConcurrentHashMap::putVal (432 bytes) 60 36 n 0 jdk.internal.misc.Unsafe::compareAndSetLong (native) 62 38 n 0 jdk.internal.misc.Unsafe::compareAndSetReference (native)

n - refers native method s - synchronized method ! - exception handling going on % - means code has been natively compiled and runs in special part of memory called "Code Cache". Also means code is run in optimal way possible.


Comment Sysout and run above program with 5000 as argument

 106  102 %     4       PrimeNumbers::isPrime @ 5 (35 bytes)
 84   81     n 0       java.lang.Module::addExportsToAllUnnamed0 (native)   (static)
 57   37     n 0       java.lang.Object::hashCode (native)   
 57   35       3       java.util.KeyValueHolder::<init> (21 bytes)
 57   38       3       jdk.internal.module.ModuleReferenceImpl::hashCode (56 bytes)
 57   39       3       java.util.HashMap::hash (20 bytes)
 57   41   !   3       java.util.concurrent.ConcurrentHashMap::putVal (432 bytes)
 58   45     n 0       jdk.internal.misc.Unsafe::compareAndSetLong (native)   
 58   46     n 0       jdk.internal.misc.Unsafe::compareAndSetReference (native) 

4th column where u see 0 to 4 meaning what type of compilation has occured it shows

0 - no compilation 4 - highest level of compilation been done and the code is placed in "Code Cache"

The compiler puts this number 0 to 4 based on how often the method is run and how much lines of code present etc. This is called Profiling.

public String myMethod({ -----------------> C1 -------> Native Level 1, Native Level 2, Native Level 3 -----------------> C2 -------> Native Level 2, Native Level 4. }

C1 and C2 are compilers, Code compiled using C2 is put in Code Cache which is easily accesible and fast by JVM. Higher the compilation tier the more optimized the compiled code should be. VM doesnot optimize to run with C2 as there is a trade off. Only frequently run is optimized. VM may assign no 3 first and if accessed very much then move to 4 and code cache.

Run with below to write jvm strategy which uses 1 to 4 tiers for compilation and other arguments..

For our case the hotspot log file is created in below

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1> 15-11-2020 17:57 317,213 hotspot_pid11388.log

-XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation

Tuning Code Cache Size


On Knowing about C1 and C2 compiler and options such as % which places code in code cache. We also know the size of code cache is limited. Some code based on usage will keep compiled and put in code cache frequently so to avoid this and improve perf we can increase the Code Cache Size -

You may see below warning

VM warning: CodeCache is full. Compiler has been disabled. This tells its better to run code in native machie code by C4 but not possible due to code cache full.

-XX:+PrintCodeCache - Prints code cache size


CodeHeap 'non-profiled nmethods': size=120064Kb used=29Kb max_used=29Kb free=120034Kb bounds [0x0000018e397c0000, 0x0000018e39a30000, 0x0000018e40d00000] CodeHeap 'profiled nmethods': size=120000Kb used=151Kb max_used=151Kb free=119848Kb bounds [0x0000018e32290000, 0x0000018e32500000, 0x0000018e397c0000] CodeHeap 'non-nmethods': size=5696Kb used=1006Kb max_used=1020Kb free=4689Kb bounds [0x0000018e31d00000, 0x0000018e31f70000, 0x0000018e32290000] total_blobs=416 nmethods=105 adapters=162 compilation: enabled stopped_count=0, restarted_count=0 full_count=0

We can set code cache size with below parameter

InitialCodeCachesize ReserverCodeCacheSize CodeCacheExpansionSize

ReserverCodeCacheSize

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1\bin>java -XX:ReservedCodeCacheSize=28m -XX:+PrintCodeCache Main 5000

CodeCache: size=28672Kb used=1189Kb max_used=1203Kb free=27482Kb bounds [0x000001a3b6a80000, 0x000001a3b6cf0000, 0x000001a3b8680000] total_blobs=417 nmethods=106 adapters=162 compilation: enabled stopped_count=0, restarted_count=0 full_count=0


n - refers native method s - synchronized method ! - exception handling going on % - means code has been natively compiled and runs in special part of memory called "Code Cache". Also means code is run in optimal way possible.

public String myMethod({ -----------------> C1 -------> Native Level 1, Native Level 2, Native Level 3 -----------------> C2 -------> Native Level 2, Native Level 4. }

C1 and C2 are compilers, Code compiled using C2 is put in Code Cache which is easily accesible and fast by JVM. Higher the compilation tier the more optimized the compiled code should be. VM doesnot optimize to run with C2 as there is a trade off. Only frequently run is optimized. VM may assign no 3 first and if accessed very much then move to 4 and code cache.

Remotely Monitoring the code cache using JConsole

Jconsole reads the process from this folder so this folder should be writable C:\Users\shant\AppData\Local\Temp\hsperfdata_shant - provide full access by using Jconsole to check code cache size jconsole will itself add 2mb of memory.

Selecting the JVM

32 bit

  • might be faster if heap < 3GB
  • max heap size = 4 GB
  • C1 (client) compiler only - start - run - finish (short run pgms) we call it client program.

64 bit

  • faster if you use long / double
  • necessary if heap > 4 GB
  • max heap size - os dependent
  • C1 and C2 (client and server ) compilers (run for longer duration - like webservers)

https://blog.joda.org/2011/08/printcompilation-jvm-flag.html

The PrintCompilation flag exists to show basic information on when Hotspot compiles methods.

java -client -XX:+PrintCompilation Main 1500

Specifying which compiler to use @ runtime

-client - instructs JVM to use C1 compiler only -server - -d64 -

Figure out how many threads are running and threshold for native compilation (number of threads that run a program determine performance -

more threads its faster right?)

a) How Threads are present to run this application

b) What is the threshold for native compilation ( how many times does a method need to get run before java decides that method should be compiled).

we can use flags to choose both of it

1 No of threads choose depends on the CPU -

To find default for your computer

2 ways - To get a default settings

  1. java -XX:+PrintFlagsFinal
  • Check "CICompilerCount " - its 3 (so there are 3 threads available for compiling our code )
  1. We can jinfo comes with JVM to figure out what is it using to run i.e number of threads memory etc..

Find process id of running program

$jps

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1>jps 11188 11860 BootLanguagServerBootApp 8760 Jps

jinfo -flag CICompilerCount 11188

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1>jinfo -flag CICompilerCount 11188 -XX:CICompilerCount=3

We got the same CICompilerCount we got above

Increasing numer of threads i.e -XX:CICompilerCount=

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1>java -XX:+PrintCompilation Main

Run with default CICompilerCount i.e 3

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1> java -XX:+PrintCompilation Main 1500

Execution time in milliseconds : 547

Increasing numer of threads i.e -XX:CICompilerCount=6

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1>java -XX:CICompilerCount=6 -XX:+PrintCompilation Main 1500

Execution time in milliseconds : 408

You can see significant decrease in execution time.

Native Compilation Tuning

-XX:CompileThreashold =n

Find out default copiler threashold

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 02\PerformanceExample1\src>jinfo -flag CompileThreshold 11188 -XX:CompileThreshold=10000

Decrease compiler threshold

java -XX:CICompilerCount=6 -XX:CompileThreshold=1000 -XX:+PrintCompilation Main 1500

Execution time in milliseconds : 404 (lower than 408 which is for CompileThreshold=10000)

Java Memory

How memory works in Java

  • Minimizing need of garbage collection improves system performance

How Stack and Heap Works

  • Memory is divided into Stack - Heap - and MetaSpace

how variable scoping works - Only primitives are stored in stack and too local variables.

in below code for all local variables it goes into the stack in LIFO - when JVM encounters } then all variables pertaining to that function is removed.

public class main{ p s v main(String args[]) { int value=7; value = calculate(value); }

public static int calculate(int data) { int tempValue = data +3; int newValue = tempValue * 2; return newValue; } }

How Heap Works

Objects are stored in heap

One heap is shared across all the threads and the number of stacks one for each thread

**Refer below the primitive is stored in Stack and pointer to heap if its an Object here String and Value stored in heap ** and also the pointer "name" is stored in Stack.

int age =21; String name="Hello";

Stack | | | |
name --|------->| String | Hello | | age=21 | |

Java Memory - The Rules

Objects are stored on the heap Variables are a reference to the object Local variables are stored on the stack

public static void main(String args[]){

List<String> myList = new ArrayList<String>();  --> Actual myList is stored in heap and reference pointer in Stack
myList.add("one");
printList(myList);

}

public static void printList(List data){ --> data refeence is stored in stack and it points to already available myList S.o.p(data);

String value = data.get(0);                     --> value ref created in stack points to myList String("one")
data.add("Four");
S.o.p(value);

}

Pass by value means what?

what happens when we create a method and pass object as argument

Pass a value as method argument copies the value of the varible . this is know as passing variable by value.

public static void main(String args[]){

int localValue=5;                    -- stored in stack as localValue=5
calculate(localValue);
S.o.p(localValue);    -- print 5

}

public static void calculate(int calcValue){

calcValue = calcValue * 100;    -- stored in stack as calcValue =500

}

Pass Object as parameter

For objects passed into methods, the REFERENCE to the object is passed by value

public static void main(String args[]){

Customer c = new Customer("Sally");   - customer object and string created in heap; a reference is created in stack "c"
rename(c);	- object pass by value -   a new reference is copied from already existing in stack and it points to customer object in heap
s.o.p(c.getName());                  - prints  "diane".

}

public static void rename(Customer cust){ cust.setName("diane"); -- creates a new string "diane" and older is releived for gc and cust points to this..cust poped from stack on completion of funtion. }

Chapter 9 : MetaSpace and internal JVM memory optimizations

Metaspace - contain detail about which code need to be converted to byte code and to native code. Metaspace is out of reach for us. static primitive is stored in metaspace where as static object is stored in heap but reference to it is stored in metaspace.

static int globalvar =3 - stored in metaspace static Map<String,String> settings = new HashMap<String,String> - map is stored in heap and setting pointer is stored in metaspace.

Note: Object from the heap which are referenced from metaspace is never garbage collected.

Metaspace is accessed by all.

Are objects always stored in heap?

Java dont give a control for us to create object in stack or heap , but JVM can store object in stack if it feels object is not shared between different methods.

String Pool and intern (deduplicate string object)

String one="hello" String two="hello"

System.out.println(one.equals(two)) -- true System.out.println(one == two) -- true

Integer i=76; String three = i.toString();

System.out.println(three.equals(four)); -- true System.out.println(three == four); -- false

Integer i=76; String three = i.toString().intern()

System.out.println(three.equals(four)); -- true System.out.println(three == four); -- true

Jvm Tuning - Flags

Tuning the size of string pool

program for benchmark

List pool=new ArrayList() for (int i=0;i<100000;i++)

java -XX:+PrintStringTableStatistics -XX:StringTableSize=120121 Main

For programs having intensive string usage increasing the StringTableSize improves the performance

Prints all options - check to see Heap Size here

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal

Setting a heap size

java -XX:MaxHeapSize=600m -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal

You can lower your heap size by using this command (-XX:MaxHeapSize=600m ) and test if your application is running perfectly.

If you get out of memory error it means memory leak in program (We figure out how fast memory fills and come up with intialheapsize)

java -XX:InitialHeapSize=1g -XX:+PrintStringTableStatistics -XX:StringTableSize=120121 Main

Using a Profiler - used to see underneeth JVM to see whats occuring

Chapter 14 : Generational Garbage Collection :

How the garbage collector works out what is the garbage?

when gc is running we get less processing power available for our application when gc is taking place, so efficient the gc is lower the application impact. objects no longer reachable are removed is what we call garbage collection. Modern garbage collection algorithms know as garbage collectors use a clever mechanism for achieving it.

Mark and Sweep

garbage collectors collects the objects which are not eligible for garbage collection and it saves them. More the garbage the gc process is faster

Mark collects live objects and put is in compact memory, while marking takes place application freezes no thread for app allowed , it is stop the world event. Sweep removes un live objects and removes.

Why the heap is divided into generations

Gc is efficient only when we have lots of object which are not pointed from stack, meaning lots of objects to garbage collection. So to make GC efficient the heap is dividend into Young and old generation. GC running on young is minor gc and on old major gc (takes more time). Actually the objects swaps between S0 and S1 keep happening through out. Objects surving young garbage collection goes into Old space in heap.

Internals of young generation

Young generation is dividend into Eden | S0 | S1 - Objects which surive gc goes into S0 on subsequent gc goes to S1. Always S0 or S1 will be empty.Reason for this is the GC need not garbage collection while young generation space. After n number of gc surviving objects get moved into "Older generation" where the gc takes long time to get garbage collected.

Chapter 13 : Analysing Heap Dump

Generating Heap Dump

ways to generate heap dump

  1. You can use JVisualVM

  2. Command line with options saying whenever the application crashes with out of memory error generate a heap dump

run CustomerHarness.java in STS by providing JVM options : -Xmx50m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\heapdump\heap

  1. From Eclipseanalyser.exe - file - Acquire Heap dump -select live java process for which u need heap dump

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=somefilepath

Generate HeapDump from JVisualVM

Run Pgm D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\Starting Workspaces\java8\Chapter 12\SoftLeaks\src\main\CustomerHarness.java with VM argument : -Xmx50m Open JvisualVM and rightcliek on above process CustomerHarness and connect go to VisualGC and see heap etc - check log of above pgm and when it is about to hit out of memory error - click on HeapDump button in "Monitoring Tab" of visualGC - and note filename where heap dump is taken. (Info button click gave me file location)

C:\Users\shant\AppData\Local\Temp\visualvm_shant.dat\localhost_11360\heapdump-1605777172209.hprof

Viewing Heap Dump


Tool to analyse Heap dump - Eclipse Memory analyser Tool - download from (https://www.eclipse.org/mat/)

download and click on -exe - MemoryAnalyser.exe - file - Open Heap dump -> location where heap dump is stored C:\Users\shant\AppData\Local\Temp\visualvm_shant.dat\localhost_11360\heapdump-1605777172209.hprof

Click on either "component report" or "Leak Suspect" based on our need. You may click on Problem Suspect and choose Shortest path to Accumulation point or Accumulated Objects in dominator tree. In these options you can see objects that are retained in heap otherwise objects which are not garbage collected.

====================== Used to verfiy how many times a garbage collection runs -Xmx10m -verbose:gc (10mb provided and -verbose:gc to see how gc is running)

Allocate 10 mb of heap and check garbage collection pattern

We see output like this

GC (Allocation Failure) --> Minor GC [Full GC (Ergonomics) 7130K ->6310K(9216K) --> Major GC

=======================================================================================================

-XX:NewRatio=4

This means we want our older generation to be 4 times to young generation i.e if heap size =10mb then old generation == 8mb and young generation = 2mb

-verbose:gc -XX: -UseAdaptiveSizePolicy

jps

8960 Main 8820 Jps 10236 Main 5100

jinfo is used to apply some policy to running JVM process

jinfo -flag UseAdaptiveSizePolicy 10236

jinfo -flag NewRatio 10236 -XX:NewRatio=2 (Older generation will be twice as big as new generation)

Split the memory for equally for young and older generations

provide the input to the progam -Xmx20m -XX:NewRatio=1

Now run the program and check to see

jps (shows process)

Check using JVisualVM or jinfo to see if it works correctly jinfo -flag NewRatio

Split the size of heap between Young generation and Survivor space

-Xmx20m -XX:NewRation=1 -XX:SurvivorRatio=5 Provides 1/5 of the Heap space allocated to Survior space

How many generations should an object survive before it becomes part of old generation. That is if we want the objects to live in young generation

longer

-XX:MaxTenuringThreshold =n

check to see

jps jinfo -flag MaxTenuringThreshold 10940 -XX:MaxTenuringThreshold=15 (Means jvm allows an object 15 times before moving it to old generation)

Choosing a Garbage Collector

Pre Java 9

3 Types of Garbage Collectors

  • Serial (when gc hapens app is on hold) -XX:+UseSerialGC
  • Parallel (-XX:+UseParallelGC - default gc for Java 9; works on young generation, minor, if we have multiple process running and for larger dataset good)
  • Mostly Concurrent (app wont pause; while mark it pause; stop the world is stopped). Below is the default for Java 9

-XX:+UseConcMarkSweepGC -XX:+UseG1GC (Default garbage collector for java 10)

Benchmarking with JMH

local code used : D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\End Of Chapter Workspaces\Chapter 18\jmhBenchmarking (Code came from this course)

http://tutorials.jenkov.com/java-performance/jmh.html

The easiest way to get started with JMH is to generate a new JMH project using the JMH Maven archetype. The JMH Maven archetype will generate a new Java project with a single, example benchmark Java class, and a Maven pom.xml file. The Maven pom.xml file contains the correct dependencies to compile and build your JMH microbenchmark suite.

Here is the Maven command line needed to generate a JMH project template: mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -DarchetypeArtifactId=jmh-java-benchmark-archetype -DgroupId=com.jenkov -DartifactId=first-benchmark -Dversion=1.0

I have taken a dirvertion - am following the course and benching marking using the code provided in the couse....

cd D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\End Of Chapter Workspaces\Chapter 18\jmhBenchmarking

The benchmarks.jar File

When you build your JMH benchmarks, Maven will always generate a JAR file named benchmarks.jar in the target directory (Maven's standard output directory). The benchmarks.jar file contains everything needed to run your benchmarks. It contains your compiled benchmark classes as well as all JMH classes needed to run the benchmark. If your benchmarks has any external dependencies (JAR files from other projects needed to run your benchmarks), declare these dependencies inside the Maven pom.xml, and they will be included in the benchmarks.jar too. Since benchmarks.jar is fully self contained, you can copy that JAR file to another computer to run your JMH benchmarks on that computer.

mvn clean install

Gives 2 artifact

test-1.0.jar (contain our @Benchmark method code we have written) benchmarks.jar JMH Benching code - we need to run this to bench mark our code)

Run

java -jar benchmarks.jar (This will take 25 mins).. java -jar benchmarks.jar -h (-h gives command line modes we can use)..

from -h option we can now find average time it takes for each method java -jar benchmarks.jar -bm avgt

Performance and Benchmarking Excercise Using Java Mission Control

We can use this if we use openjdk or oraclejdk licensed version JMC is opensorce which is part of openjdk - we can use flight mode etc in here which was licensed before.

https://www.oracle.com/java/technologies/javase/products-jmc7-downloads.html downloaded : D:\softwares\jmc-7.1.2_windows-x64

How to run JMC

D:\softwares\jmc-7.1.2_windows-x64\bin>jmc -vm "C:\Program Files\Java\jdk1.8.0_271\bin"

2 modes -

mbean - Used to see live stats.. flight mode - Run and check the stats later -

You can choose continous recording which we need to do in production system and we can dump the result to file and the analyse it and it opens you can dump whole recoding or part .

MBean - Click on this..

You have + symbols in Dashboard where you can see different Dials like Heap memory, CPU dials shows quick information of the system, where you can add new Dial, add additional attribute to processor or memory You can Press + Symbol to add new Dial (Example: Eden Space), Add new Graph (Example: Memory used)

MBean Browser

different way of viewing stats You can right click and visualize this meaning you can add to dashboard and other places...

system, memory and diagnostic command tab

memory shows gc info diagnostic command tab - list of commands we can use against jvm - select the click on "Execute" - it shows the detail VM.version - shows our jvm version VM.flag shows all the flags STS uses when starting our program

Flight Recording

You will need to run your progrm with below parameters

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder

UnlockCommercialFeatures - for oracle jvm with license

FlightRecorder - for openjdk

Run the JMC 1st

We have used JVisualVM to check the heap and garbage collection etc but to need to focus on CPU behaviour, disk usage, network so to how what is happening in the jvm we need to use a profiler. it is a seperate application that connects to java vm and so we can review. It slows down jvm beware.

JMC is a profiler used to You can see application info, JVM internals etc.

D:\softwares\jmc-7.1.2_windows-x64\bin>jmc -vm "C:\Program Files\Java\jdk1.8.0_271\bin"

Import below project into workspace

Run the main program in the below and in the JMC Start the Flight Recording for 1 minute - double click on main and flight recording....once it completes it will show result.

D:\Udemy_App-Perf-MemoryMgmt\PracticalsAndCode\End Of Chapter Workspaces\Chapter 17\FibonnaciPrimesImproved

After 1min i can now see the result of flight recording now....

RED ones are the error....

Install Graalvm

Download graalvm : https://github.com/graalvm/graalvm-ce-builds/releases

GraalVM Benefits with respect to performance

  • Alternative JVM
  • Alternative Java Compiler
  • Native Compiler (no jvm reqired)

cd /d/softwares/graalvm-ce-java8-windows-amd64-20.3.0/graalvm-ce-java8-20.3.0/bin

Areas of focus in performance perspective

  • At coding level what can be done to improve performance

-run frequently running code in code cache

  • docker image is built out of CI phase so only by looking we can suggest

  • logback can be used instead of log4j to improve performance

  • appropriate datastructure used update/search/add.

  • minimize json page size

Master the Coding Interview: Data Structures + Algorithms

Effecient DataStructures in Java

O(1) - Constant time O(n) - Linear Time

                   lookup       push       insert      delete

Array - O(1) O(1) O(n) O(n)

Finding best datastructure to use in Java - Excerts from book think datastructure

This book presents three topics:

source code : https://github.com/vidyasekaran/ThinkDataStructures

Data structures - Learn how to use data structures like lists and maps, and you will see how they work.

Analysis of algorithms - I present techniques for analyzing code and predicting how fast it will run and how much space (memory) it will require.

Information retrieval - To motivate the first two topics, and to make the exercises more interesting, we will use data structures and algorithms to build a simple web search engine.

Here’s an outline of the order of topics:

• We’ll start with the List interface and you will write classes that implement this interface two different ways.

Then we’ll compare your implementations with the Java classes ArrayList and LinkedList.

chapter 2 : Analysis of Algorithms

Constant time An algorithm is constant time if the runtime does not depend on the size of the input. For example, if you have an array of n elements and you use the bracket operator ([]) to access one of the elements, this operation takes the same number of operations regardless of how big the array is.

Linear An algorithm is linear if the runtime is proportional to the size of the input. For example, if you add up the elements of an array, you have to access n elements and perform n – 1 additions. The total number of operations (element accesses and additions) is 2n – 1, which is proportional to n.

Quadratic An algorithm is quadratic if the runtime is proportional to n2. For example, sup‐pose you want to check whether any element in a list appears more than once. A simple algorithm is to compare each element to all of the others. If there are n elements and each is compared to n – 1 others, the total number of comparisons is n2 − n, which is proportional to n2 as n grows.

⚠️ **GitHub.com Fallback** ⚠️