Java virtual machines and garbage collection mechanisms (i)

Source: Internet
Author: User
Tags exception handling garbage collection
This article is my reference to a number of technical articles after finishing their own summary of content Preface

Many people like to compare Java and C to determine which language is better, in fact, I think C and Java should belong to two completely different areas, C is more inclined to hardware, efficiency, and Java is more inclined to business, security concurrency and so on, Java has a strong portability, Regardless of the underlying operating system and a variety of driving environment, with a compile-and-run features, C more inclined to play the machine's performance


What is a Java Virtual machine (JVM)?

JVM Full name-java virtual Machine translation is the meaning of Java Virtual machine, we all know that Java is a Cross-platform language, and the most important feature of the foundry Java is the Java Jvm,java program's overall implementation process is as follows



As can be seen from the diagram, the Java implementation is not directly compiled into binary but into bytecode files, what is the byte code it. We can understand that only the JVM can understand the operation of the Code, which is the most different from the C language, Java compiled bytecode files must rely on the JVM virtual machine to explain to run, which tells us why Java has so good cross-platform capabilities, So you can understand that the JVM is basically the cornerstone of all Java programs.


What is garbage collection mechanism.

We all know that the program is running when the need to use memory, and memory is a limited amount of valuable resources, the program after the execution of code fragments, need to release the memory space not needed, make room for the subsequent code to run, traditional C and other languages are the programmer to allocate free resources, For example, a code fragment programmer needs to call the constructor to allocate memory to the object, and then call the destructor to release the object after the business code has finished executing. Java takes this task to a Java virtual machine JVM to execute without the need for character manipulation


It can be seen in the Java GC thread tracking the operation of the Code, and itself can trigger garbage collection, the GC provided by the JVM, is a daemon thread, responsible for monitoring and triggering garbage collection GC full asynchronous execution


memory structure of the JVM

To better understand the Java program run and the Java garbage collection mechanism, let's take a look at the JVM's memory model.


As you can see from the diagram, the largest area of the JVM is in the runtime data area (Runtime), and basically the data for the program code will be saved to the area, where the structure of the area is:


As you can see from the diagram, the runtime data area is split into multiple areas at run time, and each zone has a specific task


Method Region:

The method area belongs to a thread-shared memory area, primarily used to store data such as class information, constants, static variables, and Just-in-time compiler-compiled code that has been loaded by the virtual machine. It is noteworthy that there is an area in the method area called the runtime (Runtime Constant pool), which is used primarily to hold the various literal and symbolic references generated by the compiler, such as Integer a = 100 (-127-128-range data) or string a = "abc" and so on, which are stored in a run-time pool after the class is loaded for subsequent use. The OutOfMemoryError exception is thrown when the method area is unable to meet the memory allocation requirements.

Heap (Java Heap):

The Java heap is also a thread-shared memory area that is created when the virtual machine is started, is the largest of the memory managed by the Java Virtual machine, is used primarily for storing object instances, where almost all object instances allocate memory, and note that the Java heap is the main area of garbage collector management. So many times it is also called GC heap, and if there is no memory completion instance allocation in the heap, and the heap can no longer be extended, the OutOfMemoryError exception is thrown.

Programs Counter (program Counter Register):

A data region that belongs to a thread-private, a small memory space that represents the byte-code line number indicator that the current thread executes. Byte code interpreter works, by changing the value of this counter to select the next need to execute bytecode instructions, branch, loop, jump, exception handling, thread recovery and other basic functions need to rely on this counter to complete.

Virtual machine stack (Java virtual Machine Stacks):

A data region that belongs to a thread-private, created concurrently with the thread, and the total is associated with the thread, representing the memory model executed by the Java method. Each method executes creates a stack frame to store information such as variable tables, operand stacks, dynamic link methods, return values, return addresses, and so on for the method. Each method from the call to the end of a stack frame in the virtual machine stack in the stack and the stack process, the program in the method declaration will be saved here. As you can see from the image below, if a program keeps calling recursion, it will cause a stack overflow in this area stackoverflowerror


Local methods Stack (Native method Stacks):

The local method stack is part of the thread- private data area , which is largely related to the Native method used by the virtual machine, and can be ignored.


End (Lifetime of object)

Basically all created objects are stored in the heap, garbage collection, the specific unnecessary objects will be released, so there are some specific objects do not need to release the need and the release of the region, so the object in the heap in a specific way to divide

The objects are divided into three main categories according to their survival cycle: young generation, old age, persistent generation

The persistence generation type mainly holds the basic information of the Java class, and basically does not involve garbage collection. The younger generation and the older generation are the key targets of the garbage collection mechanism. In the younger generation, a number of areas were divided into the Eden area and two survivor districts. Object allocations created are first placed in the Eden area, the survivor area as a buffer for the Eden area and the old areas. When the technology survives, the Eden area will be put into the survivor area, and the objects in the Survivor area, which will survive a number of collections, may be transferred to the former.


Young generation:

All newly generated objects are first placed in the younger generation. Most of the younger generation are objects with shorter life cycles. There are three districts in the young generation. An Eden area, two survivor districts. Most objects are generated in the Eden area. When the Eden Zone is full and triggers garbage collection, the surviving object will be copied to the Survivor area, and when the survivor is full, the surviving object of the area will be copied to another survivor area, and when the survivor is full, Objects copied from the first survivor area and still alive at this time will be replicated in the old age area (tenured). Note that the two areas of survivor are symmetrical and have no precedence, so there may be simultaneous objects in the same zone that are replicated from Eden, and those that were copied from the previous survivor, and those that were copied to the old age only came from the first survivor. Moreover, one of the survivor areas is always empty. At the same time, the Survivor area can be configured as multiple (more than two) depending on the program, which can increase the presence of objects in the younger generation and reduce the likelihood of being placed in the older generation.


older generation:

In the younger generation, those who survived after N garbage collection will be placed in the older generation. Therefore, it can be considered that the older generation of the storage of some of the longer life cycle objects.


Persistent Generation:

Used to store static files, Java classes, methods, etc. today. Basically, garbage collection does not participate and does not affect garbage collection



trailer: Java Virtual machine and garbage collection mechanism (ii) will be detailed, young generation, old age, persistent generation and introduce GC garbage collection methods


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.