High Performance Java Best Practices
High Performance Java Best Practices
CONTENTS INCLUDE: . Java Memory Architecture . Runtime Memory Allocation . Runtime Code Execution . Performance Concerned Code . Summary JAVA MEMORY ARCHITECTURE Based on Java 5 specification, there are five memory pools in java virtual machine categorized in heap and non-heap memory.
Heap Eden Space Survivor Space Tenure Space Non-heap Permanent Generation Code Cache Pool from which memory initially allocated for all new objects Pool containing objects that have survived GC of Eden space Pool containing objects that have existed for some time in the survivor space. Hold the metadata of classes that have been loaded or created Memory used for compilation and storage of native code
Heap and Non-heap memory are created on machine startup and shared among all threads.
Memory area Heap size Perm Gen size Code Cache JVM parameters to configure -Xms, Xmx, -Xmn, -XX:NewRatio, -XX:NewSize, -XX:MaxNewSize, -XX:SurvivorRatio, -XX:MinHeapFreeRatio, -XX:MaxHeapFreeRatio -XX:PermSize, -XX:MaxPermSize -XX:ReservedCodeCacheSize
RUNTIME MEMORY ALLOCATION As main thread of application started Class Loader will load the classes metadata and save them to Perm Gen area. Any new instance of object created will be put in Eden space. Garbage Collector will traverse this space for collecting and trash non-referenced objects periodically. The objects that are survived from GC in Eden space will be moved to Survivor space. Objects that reach a certain age (also means survived from several GCs) in Survivor space will be tenured into old space/tenure space. RUNTIME CODE EXECUTION Aside to heap and non-heap memory, there is a small memory that is used for op-code execution called Java Stack, that stores frames (data, operand and partial result) plays a part in method invocation and return. Java stack frame holds the variable value - value of primitive variable and memory location (reference) in heap memory of object. Why the object instance is not loaded into stack, why only the reference? Because stack frame is designed as small as possible. As main thread of application is started, a new thread is created in virtual machine. The main thread usually will need to create another thread for a fully functioned application. Those threads have separated private Java Stack that no other thread may access another threads stack. NOTE: Stack is faster than heap but has smaller capacity.
Reference Card
Performance Concerned Code continued g) Do not use exception to control flow / business logic. Exception should be only for unpredicted error. Raising an exception needs stack unwinding for stack tracing. h) Remove unused variables (instance and local), unused method and unused method parameter. Use a tool to help creating high quality code. IntelliJ IDEA (now offers free version) has very handy features i.e. intention and code inspection that help you to clean the code. i) Watch the runtime behavior of application in virtual machine. Use a profiler tool to know (or validate) the profile of running application. SUMMARY There are more some best practices related to choosing the Java API classes and technique of initialization to boost performance of java application. The list above is related to memory and processor efficiency. We are lucky being provided a very huge memory in a PC nowadays. But doesnt mean there is no need to use it efficiently. Do you know that the less memory and CPU process, the less energy consumed by computer? Go green & achieve high performance code at the same time.
Leonar Tambunan is a software engineer, architecting and java performance tuning enthusiast. Reach him at [email protected]