Posts

Showing posts with the label JVM

How Serial Garbage Collector works in the HotSpot virtual machine

Image
The Serial Garbage Collector was one of the first garbage collectors in the HotSpot VM. While this collector is running, the application is suspended and continues to run only after garbage collection has stopped. Application memory is divided into three spaces: Young generation . Objects are created in this particular area of memory. Old generation . Objects that survive "minor garbage collection" are moved to this area of memory. Permanent generation . Here metadata about objects, Class data sharing (CDS), String pool are stored. The permanent area is divided into two: read-only and read-write. Obviously, in this case, the read-only area is never cleaned up by the garbage collector. The Young generation memory area consists of three areas: Eden and two smaller Survivor spaces - To space and From space. Most objects are created in the Eden area, with the exception of very large objects that cannot be placed in it and therefore are immediately placed in the Old ge...

What flavors of garbage collectors are implemented in the HotSpot virtual machine

Image
The Java HotSpot VM provides developers with four different garbage collectors to choose from: Serial is the easiest option for applications with low data volume and low latency requirements. At the moment, it is used relatively rarely, but on weak computers it can be selected by the virtual machine as the default collector. The use of Serial GC is enabled with the -XX:+UseSerialGC option. Parallel - inherits assembly approaches from the sequential collector, but adds parallelism to some operations, as well as the ability to automatically adjust to the required performance parameters. The parallel collector is enabled with the -XX:+UseParallelGC option. C oncurrent Mark Sweep (CMS) - aims to reduce maximum latency by performing some of the garbage collection work in parallel with the main threads of the application. Suitable for dealing with relatively large amounts of data in memory. The use of CMS GC is enabled with the -XX:+UseConcMarkSweepGC option. Garbage-First (G1) - de...

How does the garbage collector work in Java

Image
Garbage collection is the process of freeing space on the heap so that new objects can be added. Objects are created using the new operator, thereby assigning a reference to the object. To finish working with an object, you just need to stop referring to it, for example, by assigning a reference to another object to a variable or the value null; terminate the execution of the method so that its local variables expire naturally. Objects that are not referenced are commonly referred to as garbage, which will be removed. The Java virtual machine, using the garbage collection mechanism, ensures that any object that has references remains in memory - all objects that are unreachable from the executable code, due to the lack of references to them, are deleted, freeing the memory allocated for them. More specifically, an object is outside the scope of the garbage collection process if it is reachable through a chain of links starting at the GC Root, i.e. a link that directly exists in the e...

What is a garbage collector for in Java

Image
The Garbage Collector only needs to do two things: Find garbage - unused objects. (An object is considered unused if none of the entities in the code currently executing contains references to it, or the chain of links that could connect the object with some application entity is broken); Free memory from garbage. There are two approaches to detecting garbage: Reference counting; Tracing Reference counting . The essence of this approach is that each object has a counter. A counter stores information about how many references are pointing to an object. When the link is destroyed, the counter is decremented. If the counter value is zero, the object can be considered garbage. The main disadvantage of this approach is the difficulty of ensuring the accuracy of the meter. Also, with this approach, it is difficult to detect circular dependencies (when two objects point to each other, but no living object refers to them), which leads to memory leaks. The main idea of the Tracing a...

Heap and Stack memory in Java

Image
Heap is used by the Java Runtime to allocate memory for objects and classes. The creation of a new object also happens on the heap. This is also the domain of the garbage collector. Any object created on the heap has global access and can be referenced from anywhere in the application. Stack is a data storage area also located in shared random access memory (RAM). Whenever a method is called, a new block is created in stack memory that contains primitives and references to other objects in the method. As soon as the method finishes working, the block is also no longer used, thereby providing access to the next method. The size of stack memory is much smaller than the amount of memory on the heap. The stack in Java works according to the LIFO scheme (Last-In-First-Out) Differences between Heap and Stack memory : The heap is used by all parts of the application while the stack is used by only one thread of program execution. Whenever an object is created, it is always stored on th...

How are JRE, JVM and JDK different

Image
JVM , Java Virtual Machine - the main part of the Java Runtime Environment (JRE). The Java Virtual Machine runs Java bytecode that is previously generated from the Java source by the Java compiler. The JVM can also be used to run programs written in other programming languages. JRE , Java Runtime Environment, is the minimum required virtual machine implementation to run Java applications. Consists of JVM and standard set of Java class libraries. JDK , Java Development Kit - JRE and a set of tools for developing applications in the Java language, including the Java compiler, standard Java class libraries, examples, documentation, various utilities. In short: JDK is an environment for developing programs in Java, which includes JRE - an environment for ensuring the launch of Java programs, which in turn contains a JVM - an interpreter for Java programs. Read also: Runtime data areas in Java Frames in JVM Execution Engine in JVM

Execution Engine in JVM

Image
The bytecode assigned to run-time data areas will be executed by the execution engine. The execution engine reads the bytecode and executes it piece by piece. Interpreter The interpreter interprets the bytecode quickly, but it is slow. The disadvantage of the interpreter is that when the same method is called multiple times, a new interpretation is required each time. JIT Compiler The JIT compiler eliminates the shortcomings of the interpreter. The engine will use the interpreter's help when converting the bytecode, but when it finds duplicate code, it uses a JIT compiler, which compiles all the bytecode and changes it to native code. This native code will be used directly for repeated method calls that improve system performance. Intermediate Code Generator. Produces intermediate code. Code Optimizer. Responsible for optimizing the intermediate code generated above. Target Code Generator. Responsible for generating machine code or native code. Profiler. A special componen...

Frames in JVM

Image
Frame is used to store data and partial results, as well as to perform dynamic binding, return values ​​for methods, and throw exceptions. A new frame is created every time the method is called. The Frame is destroyed when the method call completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are popped off the stack of the thread creating the frame. Each frame has its own array of local variables, its own operand stack, and a reference to the constant pool during the execution of the current method's class. The sizes of the local variable array and operand stack are determined at compile time and provided with the code for the method associated with the frame. Thus, the size of the data structure, frame, depends only on the implementation of the Java virtual machine, and the memory for these structures can be allocated simultaneously when the method is called. Only one frame is active at any point in a given control flow, the execution me...

Runtime data areas in Java

Image
The JVM allocates many areas of data at runtime that are used at runtime. Some chunks of data are created by the JVM at startup and destroyed during shutdown. Others are created for each thread and destroyed when the thread is destroyed. The pc Register (PCR) The Java Virtual Machine can support many threads of execution concurrently. Each thread of the Java virtual machine has its own PC register (programm counter). At any time, each Java Virtual Machine thread is executing the code of one method, namely the current method for that thread. If this method is not native, the pc register contains the address of the Java virtual machine instruction that is currently executing. In short: there is one PCR for one thread, which is created when the thread starts. The PCR stores the address of the JVM instruction currently executing. Java Virtual Machine Stacks Each thread in the JVM has its own stack created at the same time as the thread. The stack in the JVM stores frames. Stacks in th...

Classloader in Java

Image
The class loader is the part of the JRE that dynamically loads Java classes into the JVM. Classes are usually only loaded upon request. The Java runtime doesn't need to know about files and file systems thanks to the class loader. Delegation is an important concept that the loader does. The class loader is responsible for finding libraries, reading their contents, and loading the classes contained in the libraries. This loading is usually done "on demand" because it does not happen until the program calls the class. A named class can only be loaded once by a given classloader. When the JVM starts up, three classloaders are used: Bootstrap class loader Extensions class loader System class loader The Bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib folder. This loader is part of the core JVM, written in native code. The extension class loader loads the code into the extension directories (<JAVA_HOME>/jre/lib/ext,...

What the JVM is responsible for

Image
Loading, checking and executing byte code; Providing a runtime environment for executing bytecode; Memory management and garbage collection; The Java Virtual Machine is a mechanism that provides a runtime environment for managing Java code or applications. A virtual machine is an independent shell for executing code, thanks to which it can be launched on any OS, without the OS affecting the program being executed. JVM works with 2 data types: primitive types and reference types. Primitives The JVM works with primitive values ​​(integers and floats). Essentially, the JVM is a 32 bit machine. The types long and double, which are 64-bit, are natively supported but occupy two memory units in frame's local or operand stack, since each unit is 32 bits. The boolean, byte, short, and char types are signed wide (except for null-extended char) and work as 32-bit integers, just like int types. Smaller types have only a few type-specific instructions for loading, storing, and convertin...