Java's memory allocation and recycling are all automatically completed by the JVM garbage collection process. Unlike C, Java developers do not need to write their own code to implement garbage collection. This is one of the many features that Java is very popular with everyone, and can help programmers better write Java programs.
This tutorial is the first part of the series. The basic terms will be explained first, such as JDK, JVM, JRE, and HotSpotVM. Next, we will introduce the JVM structure and the Java heap memory structure. Understanding these basics is important for understanding the subsequent garbage recycling knowledge.
Key Java Terms
JavaAPI: A series of encapsulated libraries that help developers create Java applications.
Java Development Toolkit (JDK): A series of tools to help developers create Java applications. JDK contains tools to compile, run, package, distribute and monitor Java applications.
Java Virtual Machine (JVM): JVM is an abstract computer structure. Java programs are written according to the features of the JVM. The JVM is specific to the operating system and can translate Java instructions into instructions that are underlying systems and execute. JVM ensures Java's platform-independent.
Java Runtime Environment (JRE): JRE includes JVM implementation and Java API.
JavaHotSpot Virtual Machine
Each JVM implementation may adopt a different approach to implementing the garbage collection mechanism. Before the acquisition of SUN, Oracle used JRockitJVM and after the acquisition, HotSpotJVM was used. Currently Oracle has two JVM implementations and the two JVM implementations will merge into one after a period of time.
HotSpotJVM is part of the current core components of the OracleSE platform standard. In this garbage collection tutorial, we will understand the garbage collection principles based on HotSpot virtual machines.
JVM architecture
The following picture summarizes the key components of the JVM. In the JVM architecture, the two main components related to garbage collection are heap memory and garbage collector. Heap memory is a memory data area used to save object instances at runtime. The garbage collector will also operate here. Now we know how these components work in the framework.
Java heap memory
It is necessary to understand the role of the JVM memory model that exists in heap memory. At runtime, Java instances are stored in the heap memory area. When an object is no longer referenced, the condition is removed from the heap memory. In the garbage collection process, these objects will be removed from the heap memory and the memory space will be recycled. The following three main areas of heap memory:
Young Generation
Eden space (Edenspace, any instance enters the runtime memory area through Eden space)
S0Survivor space (S0Survivorspace, instances that exist for a long time will be moved from Eden space to S0Survivor space)
S1Survivor space (Instances that exist for a longer period of time will be moved from S0Survivor space to S1Survivor space)
Old Generation instances will be promoted from S1 to Tenured (Lifetime Generation)
Permanent Generation contains meta information about classes, methods and other details
The permanent generation space has been removed in the Java 8 feature.
Summarize
The above is the entire content of this article briefly introduces the Java garbage collection mechanism, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!