Java 8 Memory Model


Introduction to Java 8 Memory Model

Java 8 memory model is very essential at the time of developing an application; we need to learn the java memory model at the time of developing, deploying, tuning, and monitoring the java application performance. As we know that in every programming language, memory is very important. In Java, we need to manage memory without any leaks at the time of developing the application to improve the performance of our application.

In java 8, we are using a java virtual machine to manage the memory. It is a computing machine that was used to enable the computer to run the java code. We are using three notions of JVM i.e. instance, implementation and specifications. At the time working with java, knowing the memory model is very important to improve the performance of our application.

When we execute any java code on our machine, we allocate separate memory to store the various parts of our code, which is referred to as JVM memory. We have a good understanding of how a JVM consumes memory based on our code and how the garbage collector works in the memory model.

Key Takeaways

  • Garbage collection in Java is a process that is used to identify and remove unused objects from memory, freeing up space for future use by another process.
  • The main feature of Java 8 is that it will include automatic garbage collection, which will run in the background.

Java 8 Memory Management

The young generation in JVM is the place where all the objects are created. At the time the young generation will be filled, then same time garbage collection is performed. The same garbage collection is called a minor GC. The java 8 memory management young generation is divided into three parts: one is Eden memory, and the other is survivor memory.

Below are the important points related to java 8 memory management as follows:

  • In java 8, memory management of newly created objects is located in the space of Eden memory.
  • At the time Eden spaced of java 8 memory is filled with the specified objects, then minor GC will perform, and the survivor objects are moved to one or more survivor spaces.
  • Minor GC is also checking survivor objects and moving them to other survivor spaces so that one survivor space is empty at any given time in order to survive the request.
  • Objects that survived in multiple GC cycles are moved to the memory space of the old generation. It is done by setting the threshold for the objects of the young generation.

The below figure shows how memory is managed in java 8 as follows: It contains the young generation and old memory as follows:

Java 8 Memory Model 1

As we can see in the above image memory of JVM is basically divided into multiple parts. At a major level, JVM heap memory is divided into two parts i.e. young generation and the old generation.

The old generation memory contains the objects which were long-lived and survived after multiple rounds of the minor GC. The garbage collection is performed in the memory of the old generation; at the time it will be full, then garbage collection is called major GC.

Java 8 Memory Structure

The JVM defines the various areas that were used at the time of code execution. Some data areas are created when the JVM starts, while others are created per thread. The memory area for each thread is created when the thread is created and destroyed when the thread exits.

The below figure shows the components of the java 8 memory structure as follows:

Model 2

1. Heap Memory/Area

The heap area represents the runtime data area from which we allocate memory for all classes and arrays; this memory is created when the virtual machine boots up. The heap storage objects are reclaimed using an automatic storage management system. The heap holds a fixed dynamic size.

2. Method Area

The method area stores the structure according to the class in a runtime constant pool. When the virtual machine boots up, this area is created. Because it is part of the heap, it will not be garbage collected. The size of the memory area may be fixed or expanded as needed.

3. Stack

Each JVM machine contains its private stack. The stack is used to store the frame. We are using the frame to store the data and partial results that were used to perform the linking. The stack holds the partial results and is used during method invocation.

4. Native Method Stacks

This stack is also called as C stack. It is supporting to the native methods. This is allocated to every thread; it was created when the thread is created. The size of these stacks is dynamic or fixed.

5. PC Registers

Every JVM contains its own PC register. At the time JVM thread executes the code by using a single method. As we know that our java application contains the native code, we have native and non-native methods used in java. The PC register contains the address of a JVM which was executing.

Java 8 Memory Model Permanent

As we know that in heap memory, young generation and old generation memory exist. Apart from the heap memory in java, there is another type of memory exists i.e. permanent generation of PermGen.

The below example shows the types of memory models in java 8 as follows:

Java 8 Memory Model 3

 

This memory was allocated in the spatial case when java heap was separated from the memory. This memory is used by the JVM to store application metadata. Garbage collection occurs in that memory as it does in other memory parts. The memory area is simply the area of space where PermGen memory stores the constructors and class structures.

This memory contains a limited size which leads to the out of memory. The default size of this memory is 64 MB on 32-bit JVM and 82 MB on 64-bit JVM. At the time of using this memory, JVM is required to change the size of the memory at the time of performing garbage collection.

Below example shows how we can configure the permanent memory in java as follows:

Permanent memory

Java 8 Memory Switch

Java provides multiple memory switches, which were used to set the memory sizes and the ratios.

Below are the memory switches used in java as follows. We are using the same as per our needs.

  • –Xms: This memory switch is used to set the initial heap size at the time of starting the JVM.
  • –Xmx: This memory switch is used to set the maximum size of the heap.
  • –Xmn: This memory switch is used to set the size of the young generation; after setting this size, rest spaces go into the old generation.
  • –XX:PermGen: This memory switch is used to set the initial size of PermGen memory.
  • –XX:MaxPermGen: This memory switch is used to set the max size of PermGen memory.
  • –XX:SurvivorRatio: This memory switch is used to set the ratio of survivor space and Eden space.
  • –XX:NewRatio: This memory switch is used to provide the old and new generation size ratio.

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of java 8 memory model?

Answer: The Java 8 memory model is used to manage the java application. We can fine-tune our application by allocating sufficient memory.

Q2. Which component is used in java 8 memory structure?

Answer: There are five components used in java 8 memory structure i.e. method area, heap area, native method stack, java vm stack, and PC registers.

Q3. What is the use of heap memory in the java 8 memory model?

Answer: This memory represents the runtime data area that was allocated from instances.

Conclusion

In Java 8, we use a virtual machine to manage memory. A virtual machine is a computing machine that allows a computer to run java code. Java 8 memory model is very important when developing applications. We must learn java memory model when developing, deploying, tuning, and monitoring the performance of java applications.

Recommended Articles

This is a guide to Java 8 Memory Model. Here we discuss the introduction, java 8 memory management, and structure. You may also have a look at the following articles to learn more –

  1. Text File in Java
  2. Java 8 forEach
  3. Java for Automation Testing
  4. Java SFTP

The post Java 8 Memory Model appeared first on EDUCBA.



Source link

Leave a Reply

Your email address will not be published.