Objective
Just looking at the code, we can only understand the sequence of execution of the program, through memory analysis, we are able to understand the program during the execution of memory allocation situation. The former is analyzed in the time dimension, and the latter is analyzed on the spatial dimension. The purpose of this paper is to match the specific code with the memory allocation in the process of execution, so that we have a clear understanding of this piece of content.
Overview
Let's start by introducing the entire program execution process. It is divided into three main steps:
- Double-click the program file to load the program into memory;
- In memory itself has the operating system code, will find the code in the main method to start execution;
- Memory management during execution.
Add:
The program file itself is placed on the hard disk, and the file name extension can be an. exe or A. class.
Memory management is the main problem in this article. Memory management will divide the memory into four areas: Heap area (heap), stack area (stack), data area (Segment), code Segment.
In general, keep in mind the following four points:
1.new out of things put into the heap area;
2. Local variables are placed in the stack area;
3. Static variables and string constants are placed in the data area;
4. Put the code in the Code area.
The above four points are the fundamental criteria, the following is the specific emphasis on the details.
Specific case classes and objects
During program execution, the code of the defined class is loaded into the in-memory code area;
The object is generated during instantiation (new).
As shown, the Code for class C is loaded into the code area, and when the main method is executed, C1 and C2 are local variables, and in the process of new C (), space is allocated in the heap area, which belongs to an object in which the space of the member variable is included in the control space of the object.
Method
When a method executes, it allocates space for the parameter of the method first, and the parameter is equivalent to the local variable, so it allocates space in the stack memory. The argument is then copied to the parameter in the way that the value is passed. When the method has a return value, the return value also has a block of temporary memory in the stack, with no name.
After the method executes, the temporary space for the assigned local variable and return value for the method disappears.
Inherited
As shown, if "Class Student extends Person", at New Student (), the space allocated to the object in the heap will have both the space of the parent object. That is, the subclass object is larger than the parent object, and the subclass object has the parent class object. Both this and the super reference, respectively, point to the entire object and the parent object therein.
static variables and string constants
The following is a section of code with static member variables and string constants:
Public classCat {Private Static intSid =0;//static member variable PrivateString name;//Member variables intId Cat (String name) { This. name = name; id = sid++; } Public void Info() {System. out. println ("My name is"+name+"No."+ID); } Public Static void Main(String arg[]) {Cat Mimi =NewCat ("Mimi");//"Mimi" is a string constantCat Pipi =NewCat ("Pipi"); Mimi.info (); Pipi.info (); }}
Only one copy of the static variable shown is present in the data area, and the string constant exists in the data area.
Polymorphic
There are three conditions for polymorphic existence: inheritance, overriding, parent-class references to child-class objects. The memory allocation condition is the same as the memory allocation when inheriting. Only the member variables of the parent class can be accessed when the upward transformation is performed, and the method of the subclass object is executed according to the actual bound subclass object when the overriding method is executed.
such as: Animal a= new Dog (); The object can only access the Name property and cannot access the Furcolor property of the child class object.
Dynamically decide which method code to execute. Both animal and Cat have enjoy methods when animal a= new Cat (), A.enjoy (), and during execution, the code that executes the enjoy method in Cat is determined.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Memory analysis