Management of Java heap in simple and simple

Source: Internet
Author: User
Tags garbage collection variables require

Introduction

Java's heap is a run-time data area in which instances (objects) of the class allocate space. The Java Virtual machine (JVM) heap stores all objects that are established by the running application, which are established through directives such as new, NewArray, Anewarray, and Multianewarray, but they do not require program code to be released explicitly. In general, the heap is responsible for garbage collection, although the JVM specification does not require special garbage collection techniques or even garbage collection at all, but because of the limited memory, the JVM has a heap managed by garbage collection at the time of implementation. Garbage collection is a dynamic storage management technology, which automatically frees objects that are no longer referenced by programs, and implements the function of automatic resource recovery according to specific garbage collection algorithms.

The significance of garbage collection

In C, the memory of an object is occupied until the program is finished running, and cannot be allocated to other objects until it is explicitly released, whereas in Java the memory becomes garbage when no object reference points to the memory originally allocated to an object. A system-level thread in the JVM automatically frees the memory block. Garbage collection means that objects that are no longer needed by the program are useless information that will be discarded. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is used by the new object later. In fact, garbage collection can erase memory fragments in addition to releasing useless objects. Memory fragmentation occurs because the object is created and the garbage collector frees up the memory space of the discarded object. Fragmentation is a free memory hole between the memory blocks assigned to an object. Defragmentation moves the heap memory occupied to one end of the heap, and the JVM assigns the sorted memory to the new object.

Garbage collection automatically frees up memory space and reduces the burden of programming. This makes Java virtual machines have some advantages. First, it can make programming more efficient. In the absence of a garbage collection mechanism, it may take a lot of time to solve a difficult memory problem. When programming in the Java language, the garbage collection mechanism can greatly shorten the time. The second is that it protects the integrity of the program, and garbage collection is an important part of the Java language security policy.

A potential drawback to garbage collection is that its overhead affects program performance. The Java virtual machine must track the objects that are useful in running programs and eventually release the useless objects. This process takes a processor's time. Second, the incompleteness of the garbage collection algorithm, some of the previous garbage collection algorithms can not guarantee that 100% of all the discarded memory. Of course, with the continuous improvement of garbage collection algorithm and the increasing efficiency of software and hardware, these problems can be solved.

Algorithm analysis of garbage collection

The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Discovering unwanted information objects, and (2) reclaiming the memory space occupied by unwanted objects so that the space can be reused by the program.

Most garbage collection algorithms use the concept of root set (Rootset), which refers to the collection of reference variables (including local variables, parameters, class variables) that can be accessed by the Java program in which the program is executing, and can use reference variables to access the object's properties and invoke the object's methods. The garbage collection preference needs to determine what is accessible from the root and what is not, and the objects from the root set are active objects that cannot be reclaimed as garbage, which also includes objects that are indirectly accessible from the root set. The root set, which can not be reached through any path, is eligible for garbage collection and should be recycled. Here are a few common algorithms.

1, reference counting method (reference counting collector)

Reference counting is the only garbage collection that does not use the root set, which uses reference counters to differentiate between surviving objects and objects that are no longer in use. In general, each object in the heap corresponds to a reference counter. When you create an object each time and assign it to a variable, the reference counter is set to 1. When an object is assigned to an arbitrary variable, the reference counter is added at 1 each time. When the object is out of scope (the object is discarded for use), the reference counter is reduced by 1, and once the reference counter is 0, the object satisfies the garbage collection condition.

The garbage collector based on reference counters runs faster and does not interrupt program execution for a long time, and it is appropriate to run the program in real time. However, the reference counter increases the overhead of program execution because each object assigns a new variable to the counter plus 1, and each time an existing object is scoped, the counter is reduced by 1.

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.