Garbage collection of Java virtual machines

Source: Internet
Author: User
Tags java se

Garbage collection of Java virtual machines

Java Technology and JVM (Java Virtual machine)

First, Java Technology Overview:

Java is a programming language and a computing platform that was first released by Sun in 1995. It is the technical foundation of Java programs, which include: utilities, games, business applications. Around the world, Java runs on more than billions of personal computers, billions of devices, and includes mobile phones and TV devices. Java is built on the Java platform by a series of key components as a whole.

Java Runtime Edition

When you download Java, you get the Java Runtime Environment (JRE). The JRE includes a Java Virtual machine (JVM), a Java platform core class file, and a class library that supports the Java platform. For running a Java program, these three parts are required.

Java Programming Language

Java is an object-oriented programming language with the following features:

    1. Cross-platform: Java program execution is first compiled into bytecode, which are stored in the class file and then loaded into the JVM. (Here's a word: what is bytecode?) The JAVAP command decompile the. class file because the program runs in the JVM, not directly on the operating system, so Java programs can run on multiple operating systems and devices.
    2. Object-oriented: Java is an object-oriented language that inherits many of the features of C + + and has been improved on this basis.
    3. Automatic garbage collection mechanism: Java programs automatically allocate and reclaim memory, so programmers don't have to worry about garbage collection issues.
    4. Rich Standard library: Java has a large number of pre-made objects available to programmers, more convenient to complete input/output, network programming, date calculation and other tasks.
Java Development Kit

The JDK is a tool set developed by Java programs that you can use to compile programs written in the Java language and then run the programs you write on the JVM. In addition, the JDK provides packaging and publishing program functionality.

The JDK and JRE share the Java programming interface (Java API). The Java API is a pre-packaged library used by Java developers to write Java programs, and the Java API provides tools to make Java program development easier, such as string manipulation, date/time processing, network programming, and implementing data structures such as linked lists, maps, stacks, queues.

Java Virtual Machine

The JVM is an abstract computer. The JVM is a program that looks like a computer that can execute programs. In this way, Java programs are written using the same interfaces and libraries. Each JVM corresponds to a specific operating system, translating Java instructions into instructions or commands that can be run on that operating system. In this way, the Java program realizes platform independence.

In fact, the JVM is ignorant of the Java language, and the JVM can recognize special binary and class file formats. A class file contains JVM directives or bytecode, symbol tables, and some ancillary information.

For security reasons, the JVM has strict rules about the syntax and structure of the encoding in the class file. However, any language with functionality, can is expressed in terms of a valid class file can is hosted by the Java VI Rtual Machine. Attracted by a generally available, machine-independent platform, implementors of other languages can turn to the Java vir Tual Machine as a delivery vehicle for their languages. (These few words don't quite understand, put it here for the time being!) )

Second, explore the structure of the JVM:

Hotspot Architecture

The architecture of the HotSpot JVM enables it to support powerful basic features and performance, and to achieve high performance and scalability. For example, the HotSpot JVM JIT compiler can dynamically optimize at compile time. In other words, these compilers can make optimal choices when the Java program is running, generating high-performance local host directives to accommodate the local host system architecture. In addition, its operating environment and multi-thread garbage collection become more and more mature, more and more durable engineering, the HotSpot JVM in large computer systems also exhibit scalability.

The main components of the JVM are: ClassLoader, runtime data area, execution engine.

Key Hotspot Components

The main components of the JVM that are related to performance are the highlighted ones.

 

There are three components and performance related in the JVM. A heap is an object data store, and this area is managed by garbage collection. Most of the debugging performance is related to the size of the heap, as well as the selection of the appropriate garbage collector. The JIT compiler has a large impact on performance, but it is seldom necessary to debug a new version of the JVM.

Garbage Collection Overview

What is garbage auto-recycling?

Garbage collection is a process of looking at heap memory, identifying objects that are still in use, and which objects are no longer used and then deleted. An object or object reference being used means that a part of the program still holds a pointer to the object or reference to the object. An object or reference that is no longer used means that it is no longer referenced by any part of the program. Therefore, the memory space occupied by unreferenced objects can be reclaimed.

In the C language, allocating and reclaiming memory is the user's manual operation, and in the Java language, memory reclamation is done automatically by the garbage collector. Garbage collection Basic Process:

Step 1: Mark

The first step is the tag. This process is used by the garbage collector to mark out the presence of that piece, which is not in use.

  

Blue indicates the referenced object, and gold indicates that the object is not referenced. At this stage of marking, all objects are scanned once (so the efficiency must be low). It is a time-consuming process if all the objects in the system need to be scanned again.

Step 2: Normal Delete

Normal delete is to delete an unreferenced object, keep the referenced object, and use the pointer to point to the deleted free space.

  

The memory collector holds an address reference to the free block to assign to the new object to use.

Step 2a: Compress delete

To further improve performance, after removing unreferenced objects, the remaining referenced objects in the compressed space. By moving the referenced objects together, the free space is aggregated together, making it easier for new memory allocations.

  

Why Divide garbage collection?

As mentioned earlier, it is inefficient to tag and compress all objects in the JVM. As more and more objects are allocated space, the increasing size of the object list makes garbage collection more and more time longer. However, according to the experience of program analysis, most objects have a short life cycle.

As the data shows, the y-axis represents the number of bytes allocated, and x represents the number of bytes reclaimed over time.

  

As you can see, fewer objects remain in memory over time. In fact, most objects have a very short life cycle, and the x-axis has a large value on the left and a huge drop in a short period of time.

Generational in the JVM

The information obtained from an object-allocated action can be used to improve the performance of the JVM. As a result, the heap can be divided into smaller chunks or sub-generations. The heap can be divided into: The new generation, the old or the old generation and the permanent generation.

  

The Cenozoic stores all new objects. When Cenozoic overflows, trigger minor garbage collection. Minor collections is the optimal choice for garbage collection in the case of a high death rate for the object. A new generation of memory space that is filled with discarded objects is recycled quickly. Some of the persisted objects age and then move to old Gerneration.

Stop the World event-all minor garbage collection are "Stop the World" events, that is, all threads will be stopped until the garbage collection operation is complete.

The old gerneration is used to store objects that have been alive for a long time. Typically, the JVM sets a threshold for the Cenozoic, and when the Cenozoic age reaches this threshold, the object is moved to the old generation. Even old generation needs to be recycled. The old generation event is called major garbage collection.

Major garbage collection is also the Stop the World event. Usually major garbage collection is slower, and the process includes all surviving objects. So for a responsive program, major garbage collection need to be minimized. It is also important to note that the length of the Stop the World event caused by major garbage collection is affected by the category of garbage collector used in the old gerneration space.

The metadata is stored on a permanent behalf, which is used by the JVM to describe the class files and methods used in the program. The old Gerneration stores the classes on which the JVM is run. In addition, Java SE class library files and methods may also be stored here.

Class files may also be recycled, and if the JVM finds that they are no longer needed, the reclaimed space is used to store which programs must be classes. All garbage collections are collected for the permanent generation of space.

Generation of garbage collection process

Now that you understand the cause of the heap generation, let's look at how different spaces affect each other. Describes the process by which objects in the JVM allocate memory and object aging:

1. First, any new objects are assigned to the Eden space. Two survivor spaces are empty at the beginning of a space.

  

2. Minor garbage collection is triggered when the Eden space overflows.

  

3. The referenced object is moved to the first survivor space. Unreferenced objects are deleted when the Eden space is emptied.

    

4. The Eden Space performs the same action the next time the GC is minor. The unreferenced object is deleted and the referenced object is moved to a survivor space. However, in this case, the referenced object is moved to S1. In addition, the age of objects stored in S0 that were generated by the last minor GC is increased and moved to S1. Once all the surviving objects have been moved to the S1, both the S0 and Eden spaces are emptied. Notice that there are now objects of different ages in the survivor space.

  

5. Repeat the same process the next time you minor the GC. However, this time the survivor space is exchanged. The referenced object is moved to the S0. The age of the surviving objects increased, and Eden and S1 were emptied.

  

6. The upgrade process is described below. After a minor GC, these objects were upgraded from the Cenozoic to LD generation when the age of the older objects reached a certain age threshold (the threshold in this example was 8).

  

7. As the minor GC continues to occur, the object continues to be moved to the old generation space.

  

8.young generation after multiple minor GC, the major GC will eventually execute on the old generation to clean up and compress this memory space.

  

Summary: The above is the basic principle of garbage collection and process, there are errors please correct me!

Attached: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

  

Garbage collection of Java virtual machines

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.