Knowledge Transfer

Ethickfox kb page with all notes


Project maintained by ethickfox Hosted on GitHub Pages — Theme by mattgraham

JAR and WAR

Java Archive : Contains class files, resources, dependent libraries, and other files needed for the application. Can contain the entry point, and be used as a target for executing the java command

Web Archive: Technically has the same structure, but another role - JavaEE web component archive. Usually contains jar-s with implementation, JSP, static front-end files, and meta information for server-container (web.xml). Mostly used as a web-application exploit in a servlet container

JVM

JDK - Application developer suite, includes JRE and Java Development Tools(compiler, archiver and etc) JRE - Package of all necessary for running compiled java program (bytecode). Includes Java Class Library and JVM JVM - The specification of the program intended to execute Java byte code. Contains:

Hotspot - JVM выпускаемая Oracle, написана на c++

JIT

JIT stands for Just-In-Time and it is used for improving the performance during run time. It does the task of compiling parts of byte code having similar functionality at the same time thereby reducing the amount of time for the code to run. The compiler is nothing but a translator of source code to machine-executable code.

  1. Java source code (.java) conversion to byte code (.class) occurs with the help of the javac compiler.
  2. Then, the .class files are loaded at run time by JVM and with the help of an interpreter, these are converted to machine understandable code.
  3. JIT compiler is a part of JVM.
    1. When the JIT compiler is enabled, the JVM analyzes the method calls in the .class files
    2. compiles them to get more efficient and native code.
    3. ensures that the prioritized method calls are optimized.
  4. Once the above step is done, the JVM executes the optimized code directly instead of interpreting the code again. This increases the performance and speed of the execution. Untitled 62.png Typically, only some paricular pieces of code are executed frequently enough in an application and the performance of the application depends mainly on the speed of execution of these parts. These code parts are called hot spots (hot spots), they are and compiles the JIT compiler. There are 2 compilers:

Reflection

The mechanism of studying information about the program at the time of its execution. Reflection allows to study and modify information about fields, methods and constructors of classes.

Method methodOfFoo = fooObject.getClass().getMethod("fooBar",null);
methodOfFoo.invoke(fooObject,null);

Using reflection has its own cons:

Reference

В java все объекты хранятся в куче, а в переменных лишь ссылки на эти области памяти. Вся передача происходит по значению. Для примитивов - копия текущего значения, для ссылок на объекты - копия ссылки Виды

Memory allocation

Mainly used by java runtime, Java Heap Space comes into play every time an object is created and allocated in it. The discrete function, like Garbage Collection, keeps flushing the memory used by the previous objects that hold no reference. For an object created in the Heap Space can have free access across the application.

Garbage collection

Garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.

Disadvantages

Whenever the garbage collector runs, it has an effect on the application’s performance.

Lifecycle

States

Generation hypothesis

The probability of death as a function of age decreases very quickly. The vast majority of objects live extremely short. The longer you live, the more likely you are to live.

Stop the world

When the garbage collector thread is running, other threads are stopped, meaning the application is stopped momentarily. Depending on the needs of an application, “stop the world” garbage collection can cause an unacceptable freeze. This is why it is important to do garbage collector tuning and JVM optimization so that the freeze encountered is at least acceptable.

GC algorithms

One of the basic was of garbage collection involves three steps:

  1. Marking: This is the first step where garbage collector identifies which objects are in use and which ones are not in use.
  2. Normal Deletion: Garbage Collector removes the unused objects and reclaim the free space to be allocated to other objects.
  3. Deletion with Compacting: For better performance, after deleting unused objects, all the survived objects can be moved to be together. This will increase the performance of allocation of memory to newer objects.

Search

Организация памяти

Object Header

mark word  - хранит вспомогательную информацию identity_hashcode - хэшкод объекта age - количество пережитых сборок мусора. Когда age достигает числа max-tenuring-threshold, объект перемещается в область heap - old generation. biased_lock - включение/выключение(Biased Locking - ускоряет повторный захват объекта тем же потоком) lock - состояние блокировки объекта

Generational Garbage Collection

Generational garbage collection can be loosely defined as the strategy used by the garbage collector where the heap is divided into a number of sections called generations, each of which will hold objects according to their “age” on the heap.

Heap Space

The heap is a large bulk of memory intended for allocation of objects.

The old generation hosts objects that have lived in memory longer than a certain “age”. The objects that survived garbage collection from the young generation are promoted to this space. It is generally larger than the young generation. As it is bigger in size, the garbage collection is more expensive and occurs less frequently than in the young generation.

Non Heap Space

[] Metaspace is a new memory space – starting from the Java 8 version; it has replaced the older PermGen memory space. The most significant difference is how it handles memory allocation.

Stack

The stack is a part of memory that contains information about nested method calls down to the current position in the program.

In Java 1.8 or earlier, JDK holds back the memory while in JDK 11+ Java releases memory to the OS.

exhaustion of the heap memory takes place if objects are created in such a manner that they remain in the scope and consume memory. The developer should make sure to dereference the object after its work is accomplished. Although the garbage collector endeavors its level best to reclaim memory as much as possible, memory limits can still be exceeded.

Making object eligible for GC
  1. Set the object references to null once the object creation purpose is served.
  2. Point the reference variable to another object. Doing this, the object which the reference variable was referencing before becomes eligible for GC.
  3. Island of Isolation Approach: When 2 reference variables pointing to instances of the same class, and these variables refer to only each other and the objects pointed by these 2 variables don't have any other references, then it is said to have formed an “Island of Isolation” and these 2 objects are eligible for GC.
public class IBGarbageCollect {
   IBGarbageCollect ib;    
   public static void main(String [] str){
	   IBGarbageCollect ibgc1 = new IBGarbageCollect();
	   IBGarbageCollect ibgc2 = new IBGarbageCollect();
	   ibgc1.ib = ibgc2; //ibgc1 points to ibgc2
	   ibgc2.ib = ibgc1; //ibgc2 points to ibgc1
	   ibgc1 = null;
	   ibgc2 = null;
	   /* 
	   * We see that ibgc1 and ibgc2 objects refer 
	   * to only each other and have no valid 
	   * references- these 2 objects for island of isolcation - eligible for GC
	   */
   }
}

GC Types

String_pool Constant_PoolString instance in Java is an object with two fields: a char[] value field and an int hash field. The value field is an array of chars representing the string itself, and the hash field contains the hashCode of a string which is initialized with zero, calculated during the first hashCode() call and cached ever since. Important thing is that a String instance is immutable: you can’t get or modify the underlying char[] array. Another feature of strings is that the static constant strings are loaded and cached in a string pool. If you have multiple identical String objects in your source code, they are all represented by a single instance at runtime.

A special space in which the strings created by "" are stored, it is in a heap.

intern() - this method can return a reference to the corresponding string in the string pool

Issues

CMS fragmentation

Дефрагментация - процесс перераспределения фрагментов файлов и логических структур файловых систем на дисках для обеспечения непрерывной последовательности кластеров. Дефрагментация требует полной остановки программы(Stop the world)

Stop the world

Пауза необходимая сборщику мусора для обхода размеченных объектов. Тк граф динамичен. Во время его обхода ранее размеченные объекты могут стать недоступными, и сборщик мусора может не удалить то, что только что стало мусором. Latency Задержка - сколько времени требуется на одну уборку мусора Пропускная способность - сколько мусора можно убрать за одну уборку https://www.digitalocean.com/community/tutorials/java-jvm-memory-model-memory-management-in-java#

GC Monitoring

jstat

We can use jstat command line tool to monitor the JVM memory and garbage collection activities. The last argument for jstat is the time interval between each output, so it will print memory and garbage collection data every 1 second.

Java VisualVM with Visual GC

GC Tuning 

Should be the last option you should use for increasing the throughput of your application and only when you see a drop in performance because of longer GC timings causing application timeout.

VM Switch VM Switch Description
-Xms For setting the initial heap size when JVM starts
-Xmx For setting the maximum heap size.
-Xmn For setting the size of the Young Generation, rest of the space goes for Old Generation.
-XX:PermGen For setting the initial size of the Permanent Generation memory
-XX:MaxPermGen For setting the maximum size of Perm Gen
-XX:SurvivorRatio For providing ratio of Eden space and Survivor Space, for example if Young Generation size is 10m and VM switch is -XX:SurvivorRatio=2 then 5m will be reserved for Eden Space and 2.5m each for both the Survivor spaces. The default value is 8.
-XX:NewRatio For providing ratio of old/new generation sizes. The default value is 2.