Java basics-Java memory management and garbage collection-java garbage collection

Source: Internet
Author: User

Java basics-Java memory management and garbage collection-java garbage collection

Java Automatic Memory Management

Before explaining memory management, you must first understand the differences between objects and object references.

An object is an instance of a class. Taking this class as an example, Person is a class we define.

Public class Person {}

Public Person person;

Person = new Person ();

While new Person () is a new object, and person is a reference to this object, which can point to any object.

1.1 java runtime region (where and where)

Program counter: it can be understood as the mark of the current thread execution position. Purpose: thread switching.

Stack

> Virtual Machine Stack: A stack frame is created when each method is executed. The method execution means that the stack frame is swapped in and out in the memory area. The method parameters and local variables are stored here. The types are basic data types, and arrays/object references.

> Local method Stack: similar to VM stacks, it only serves native methods. Image Reference 2.

 

Heap:Stores object instances. Garbage collection mainly refers to the garbage collection policy for the heap.

Method Area: class information, constant, static variable (static, class), including constant pool

1.2 garbage collection mechanism

Garbage Collection (GC) automatically clears objects that are no longer used in the heap.

If an object is not referenced, we call this object inaccessible. Garbage collection is used to release the memory occupied by inaccessible objects. This is the basic principle of garbage collection.

Garbage collection involves two steps: determining whether the object is dead (not reachable) and clearing it.

1.2.1 is the object dead?

> Reference count. Add a counting quotor to the object. If the value is 0, the object cannot be reached. Disadvantage: the problem of mutual reference cannot be solved. ObjA. instance = ObjB; ObjB. instance = ObjA; ObjA and ObjB cannot be accessed, but the reference counting method cannot notify the garbage collection mechanism.

Improvement: using the stack and static data as the root (root), starting from the root and following all references, you can find all reachable objects. That is to say, a reachable object must be referenced by the root or another reachable object. Example: Reference 2

 

1.2.2. Garbage Collection Policy

    • Mark-sweep Algorithm: mark who is not reachable, and then delete.

Disadvantages: a. efficiency. The efficiency of both steps is not high; B, resulting in a large amount of space fragments.

    • Copy-clear: divides the memory into two areas A and B, scans A, copies accessible objects to B, and then clears. Disadvantage: the cost is too high.

Improvement: because most objects have A short life cycle, the memory is divided into A, B, and C according to A certain proportion (usually), and the accessible objects A and B are stored in C, clear A and B, and A and C as the and B of the previous step.

    • Marking-sorting: The marking process is similar. The surviving object moves to one end.

Reference: 1, http://jingyan.baidu.com/article/a501d80cf734c3ec630f5e25.html

2, http://www.cnblogs.com/vamei/archive/2013/04/28/3048353.htmlkey-value recommendation

3. in-depth understanding of Java Virtual Machine: jvmadvanced features and practices

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.