Schematic Java Memory recycling mechanism

Source: Internet
Author: User

In Java, its memory management consists of two aspects: memory allocation (when creating Java objects) and memory recycling, both of which are done automatically by the JVM, which reduces the learning difficulty of Java programmers and avoids the danger of direct manipulation of memory like C + + +. However, because memory management is entirely the responsibility of the JVM, many programmers in Java are no longer concerned with memory allocation, causing many programs to be inefficient and consume memory. As a result, Java programmers should finally be able to understand the JVM in order to write programs that are more efficient and take full advantage of limited memory.

"If you want to learn more about the JVM, take a look at this forum's Welcome to the course" deep JVM kernel "online video course, the only JVM video tutorial on the market today! Http://edu.51cto.com/course/course_id-1952.html


1.Java in-memory state

First, let's write a code for an example:

Package test;

Import java.io.Serializable;

public class Person implements Serializable {

Static final Long serialversionuid = 1L;

String name; Name

Person friend; Friends

Public person () {}

Public person (String name) {
Super ();
THIS.name = name;
}
}

Test.java

Package test;

public class test{

public static void Main (string[] args) {
person P1 = new Person ("Kevin");
person P2 = new Person ("Rain");
Person P3 = new Person ("Sunny");

P1.friend = p2;
P3 = p2;
P2 = null;
}
}

The object reference in the main aspect of the above Test.java is drawn as an object reference graph that starts with the main method (vertices are objects and references, and there is a reference relationship to the edge):

650) this.width=650; "class=" AlignCenter "title=" Java Memory recycling mechanism "src=" http://www.codeceo.com/wp-content/uploads/2014/ 09/fd456406745d816a45cae554c788e754.png "alt=" Java memory recovery mechanism "style=" padding:4px;margin:2px auto;list-style:none; border:1px solid RGB (203,205,204); clear:both;/>

When the program is running, it can be divided into three types after the state of the memory is considered as a forward graph:

1) up to state : After an object is created, there is more than one reference variable referencing it. In a graph, you can navigate from the starting vertex to the object, and it will be in the reach state.

2) Recoverable State : If an object in the program no longer has any reference variable referencing it, it will first enter the recoverable state, at which point the starting vertex of the graph can no longer navigate to the object. In this state, the system's garbage collection mechanism prepares to reclaim the memory used by the object, and the system calls the Finalize () method to clean up the resource before it is reclaimed, and if the resource is collated and re-allows more than one reference variable to refer to the object, the object becomes reachable again. Otherwise it will enter the unreachable state.

3) Unreachable State : When all associations of an object are cut off, and the system calls the Finalize () method to clean up the resource without making the object reachable, the object will permanently lose its reference and become unreachable. The system is really going to reclaim the resources that the object occupies.

The conversion diagram of the above three states is as follows:

650) this.width=650; "class=" AlignCenter "title=" Java Memory recycling mechanism "src=" http://www.codeceo.com/wp-content/uploads/2014/ 09/015f3dbf2674c472dcd1a357881acd87.png "alt=" Java memory recovery mechanism "style=" padding:4px;margin:2px auto;list-style:none; border:1px solid RGB (203,205,204); clear:both;/>


Schematic Java Memory recycling mechanism

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.