J2SE Fast Advanced--java Memory analysis

Source: Internet
Author: User

process of program execution

To analyze memory in Java, let's look at the program's execution process:

As shown, it is broadly divided into 3 steps:

1, in the beginning, our program is in the hard disk, when the startup run, the program will be loaded (load) into memory, where the memory can be regarded as our memory bar;

2, at this time, in addition to the memory of the program has just loaded code, there is the operating system itself code (OK, this sentence can be used as nonsense →_→), the operating system will find the program's main method to start the execution of the program;

3, the third step is the focus of this article, the system in the process of implementation of memory management. In Java, memory is roughly divided into four--heap (stacks), stack(heap), data segment(segment), code segment(snippet), It is used to store local variables in the program, new objects or arrays, static variables, and program code.

The main discussion here is the heap andstack(heap).

data types for Java

To review Java data types, there are two types of data in Java, basic data types and reference data types, such as:

The basic data types are eight, and reference data types include classes, arrays, interfaces, and so on.

( beginners may confuse string, integer and other basic data types, such as char, int , to illustrate that integer is equivalent to the "wrapper class" of int, and string can be considered an array of type char[] , in addition, Byte, float, and so on. So these types should be treated as reference types. )

Memory Analysis

As shown in the first diagram, when the program runs, the local variables that we define are generally stored in the stack memory, which can be variables of the basic data type (the variables of the base data type are stored directly in the stack), It can also be a variable of a reference type (a variable of a reference type is stored in the stack as the address of the object in the heap memory it points to).

Heap memory is the object that the address of the reference type variable points to.


practice out of the truth, the following specific analysis in the code, I hope you can spare 10 seconds to take a closer look at this code to continue:       

public class Memoryanalysis {public static void main (string[] args) {person        person=new person ("xiaoming", 15); String newname= "Little Red"; int Newage=18;person. SetName (newName);p Erson. Setage (newage);p Erson. SayHello ();}} public class Person {public String name;public int age;public person (String Name,int age) {this.name =name;this.age=age;} public void SetName (String name) {this.name=name;} public void Setage (int.) {this.age=age;} public void SayHello () {System.out.println ("My Name is" +name+ ", I" +age+ "years old");}}


The following is a step-by-step analysis of the code in the Main method:

Person Person=new person ("xiaoming", 15);

After instantiating the person class:

Heap memory: Allocating a chunk of memory in the heap memory is used to hold the data in person instance person, because the name property of person is a string type, so in the heap memory is allocated another piece of memory to hold the string type "Xiaoming", The name in person holds only one address (address 3), which points to a block of memory that holds "Xiao Ming", and the age attribute in person is an int type, so that the memory unit of type int is "15".

Stack memory: Because the person is a reference type, the memory unit that is allocated in the stack memory is the address (address 1) that points to the person instance in the heap memory. If the above code simply defines the person and no new, then only the memory unit of the person is allocated in the stack memory, and the value in the can is null.


String newname= "Little Red";

Because the newname type is string, the actual contents of the newname are also stored in the heap memory, and the memory cells in the stack memory allocation newname only the addresses that point to the "Little Red" in the heap memory.


int newage=18;

The NewAge type is of type int, so the value (18) is stored directly in the unit newage of the stack memory allocation.

Person. SetName (NewName);

The function is a little bit more complicated because the SetName (String name) function has a type of reference type parameter name, and the passed-in argument is newname, and the value stored in newname (address 2) is assigned to the name. So at this point the NewName and name store the same address (that is, pointing to "Little Red"). Similarly, when This.name=name is executed in the SetName (String name) function, the value stored in name in the stack memory (address 2) is assigned to the name of the person in the heap memory. In this case, the name stored in the person is also the address pointing to "Little Red".


After that, the person's original name "Xiaoming" will be reclaimed at some point by the Java garbage collection mechanism.

When the method finishes executing, the memory of the variable name in the stack memory is recycled.


Person. Setage (NewAge);

As with SetName (String name), execution also allocates memory units for the parameter age in stack memory, except that the formal parameter age in the setage (int age) function is type int, So directly in the stack memory allocation unit directly stored the value of the argument (18).

when the method finishes executing, the memory of the variable age in the stack memory is recycled.

The above is the execution of these lines of code. How much you know some Java basics you have already guessed the results of the operation:

Operation Result:





J2SE Fast Advanced--java Memory analysis

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.