cool hit counter Java Memory Analysis_Intefrankly

Java Memory Analysis


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/42179387

The process of execution of the program

To analyze memory in Java, let's first understand the process of program execution.

As shown in the diagram above, there are roughly 3 steps.

1、at the outset, me Our programs are stored on the hard drive., When starting a run, The program will be loaded(load) Go to memory., The memory here can be seen as me Our memory sticks;

2. At this point, in addition to the code of the program just loaded, there is also the code of the operating system itself in memory (well, this sentence can be taken as nonsense →_→), and the operating system will find the Main method of the program to start executing the program.

3. The third step is the focus of this paper, the system's management of memory during program execution. In Java, memory is roughly divided into four blocks - heap (stack), stack (heap), data segment (data segment), and code segment (code segment), which are used to store local variables, new out objects or arrays, etc., static variables, and program code in the program, respectively.

The main discussion here is on heap (stack) and stack (heap).

Java's data types

Let's start by reviewing Java's data types, which are divided into two types in Java, basic data types and reference data types, as follows.

These are the eight basic data types, and the reference data types include classes, arrays, interfaces, etc.

(Beginners may confuse String, Integer and other types with char, int and other basic data types, here to explain, Integer is equivalent to int "wrapper class", String can be seen as char [] type array, in addition, Byte, Float and other similar. So these types should be treated as reference types. )

Memory Analysis

As the first diagram shows, when the program runs, the local variables we define are generally stored in stack memory, and these local variables can be either variables of the basic data type (a variable of the basic data type holds its value directly on the stack) or variables of the reference type (a variable of the reference type holds on the stack the address of the object in heap memory to which it refers).

Heap memory holds the object pointed to by the address of the reference type variable.

Practice makes perfect, here's a breakdown specifically in the code, and I hope you'll take 10 seconds to look at this code carefully before continuing.

public class MemoryAnalysis {
	public static void main(String[] args) {				
        Person person=new Person(" Simon",15);
		String newName=" small red";
		int newAge=18;
		person.SetName(newName);
		person.SetAge(newAge);
		person.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 age){
		this.age=age;
	}
	public void SayHello(){
		System.out.println(" My name is..."+name+", me"+age+" It's a year old.");
	}
}

the following pairs ofMain The code in the method is analyzed sentence by sentence :

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

After instantiating the Person class.

heap memory: A block of memory is allocated in heap memory to holdPerson an actual exampleperson The data in the, owing toperson ofname attributes areString types, So a separate piece of memory is allocated in heap memory for theString type“ Simon”,person hit the targetname All that is stored in it is an address( address3), This address points to the address where the“ Simon” The memory block of the;person inage attributes areint types, So directly in theage The memory cell that holds theint type“15”。

Stack memory: because person is a reference type, the memory unit person allocated in stack memory holds an address (address 1) that points to an instance of person in heap memory. If the above code just defines person without new, it will just allocate a memory unit of person in the stack memory with empty values in the internal talent.

String newName=" small red";

owing tonewName The type isString types, consequentlynewName The actual contents are also stored in heap memory, Memory cells allocated by stack memorynewName in the heap memory is just a storage pointing to the“ small red” address。

int newAge=18;

The type of newAge is of type int, so the value (18) is stored directly in the stack memory allocated cell newAge.

person.SetName(newName);

When you get to the function piece, It's gonna be a little complicated., owing toSetName(String name) function has one parameter of type reference typename, And the incoming real parameter isnewName, at this timenewName The value stored in the( address2) will assign a value to thisname, So at this pointnewName harmonyname The same address is stored( That is, pointing both to“ small red”)。 for the same reason, When the execution of theSetName(String name) function in thethis.name=name time, will take the stack memory ofname The value stored in the( address2) Assign a value to the heap memory inperson ofname, this timeperson hit the targetname is also stored in the“ small red” address。

later,person formername happen to“ Simon” It will at some point bejava The garbage collection mechanism of the。

After the method is executed, the memory occupied by the variable name in the stack memory is reclaimed.

person.SetAge(newAge);

Like SetName(String name), the execution also allocates memory cells in stack memory for the formal parameter age, except that the formal parameter age in the SetAge(int age) function is of type int, so it is sufficient to store the value of the real parameter (18) directly in the cell allocated in stack memory.

After the method is executed, the memory occupied by the variable age in the stack memory is reclaimed.

The above is how these lines of code are executed. If you know more or less the basics of java, you must have guessed the result of the run.

Results of the run.


Recommended>>
1、NTT Japan launches AI confirmation service for online reservation orders
2、An eightman battle royale More flare gun updates
3、22 minutes straight to second place in the Kaggle competition An article to teach you how to do it
4、Dont be afraid to forget your directional lights TESLAs new patent automatically helps you adjust
5、SET shared community Internet Finance sharing economy of the newest work

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号