Java divides memory into two types: one is stack memory and the other is heap memory. Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function, and when a variable is defined in a block of code, Java allocates the memory space for that variable in the stack when the scope of the variable is exceeded ( For example, call function B in function A, define variable A in function B, the scope of variable A is only function B, and after function B is finished, variable A is automatically destroyed.) The memory allocated to it is reclaimed, and Java automatically frees up the allocated memory space for that variable, which can be used as an immediate alternative.
heap memory is used to store the objects and arrays created by new , and the memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. after creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable in the stack can then be used in the program to access an array or an object in the heap, which is equivalent to a name that is an array or an object. A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time. This is the reason why Java compares memory, in fact, the variables in the stack point to the variables in the heap memory, this is the pointer in Java!
Code Instance Test01: Single object creationView Code
class person{
String name;
int age;
Public void tell () {
System.out.println ("Name:" +name+ "age" +age);
}
}
Public class Test01 {
Public Static void Main (string[] args) {
Person per=New person ();
}
}
An object per is instantiated in the above program, and in the process of instantiating the object it is necessary to open up space in memory, which includes the stack memory and the memory. The specific memory allocations are as follows:
We can find that the object name per is stored in the stack memory ( more accurately, the memory of the heap is stored in the storage space of the access address ), and the specific content of the object, such as the property name and age, is stored in the heap memory. Because the per object is only instantiated and not specifically assigned, it is the default value. The default value for a string is 0 for the Null,int type. As mentioned earlier, heap memory space must be opened using the New keyword.
Code instance Test02: multiple object creationView Code
class person{
String name;
int age;
Public void tell () {
System.out.println ("Name:" +name+ ", Age:" +age);
}
}
Public class Test02 {
Public Static void Main (string[] args) {
Person per1=New person ();
Person per2=New person ();
Per1.name= "Zhang San";
per1.age=30;
Per2.name= "John Doe";
per2.age=33;
Per1.tell ();
Per2.tell ();
}
}
Key concepts: Like arrays, classes are reference types, which refer to a heap of memory that can be pointed to by multiple stacks of memory at the same time. Let's take a look at a simple instance of reference passing.
Code instance Test03: Object reference pass 1View Code
class person{
String name;
int age;
Public void tell () {
System.out.println ("Name:" +name+ ", Age:" +age);
}
}
Public class test03{
Public Static void Main (string[] args) {
Person per1=New person ();
Person Per2=per1;
Per1.name= "Zhang San";
per1.age=30;
per2.age=33;
Per1.tell ();
Per2.tell ();
}
}
The result of the program operation is:
Name: Zhang San, age: 33
Name: Zhang San, age: 33
From the running results of the program can be found, the content of two objects output, in fact, the so-called reference Pass, is the use of a heap of memory space to a plurality of stack memory space, each stack of memory space can modify the contents of the heap memory space , the program's memory allocation diagram is as follows:
Note: the object Per2 in the above instance has no heap memory space, because the object Per2 only declares the operation and does not instantiate. There will be no memory space until you instantiate using the New keyword.
Code instance Test04: Object reference pass 2View Code
class person{
String name;
int age;
Public void tell () {
System.out.println ("Name:" +name+ ", Age:" +age);
}
}
Public class Test04 {
Public Static void Main (string[] args) {
Person per1=New person ();
Person per2=New person ();
Per1.name= "Zhang San";
per1.age=30;
Per2.name= "John Doe";
per2.age=33;
Per2=per1;
Per1.tell ();
Per2.tell ();
}
}
The result of the above program operation is:
Name: Zhang San, age: 30
Name: Zhang San, age: 30
The output from the program can be found to be Test03. However, there have been some changes in memory allocations, as follows:
Note the point:
- Java itself provides a garbage collection mechanism (garbage COLLECTION,GC), will not periodically cast unused memory space, as long as the object is not used, will wait for the GC to free up space, such as the above heap in memory of the Name= "John Doe"; age=33.
- A stack of memory can only point to a memory space, if you want to point to other heap memory space, you must first break the existing point to assign a new point.
Areas of memory commonly used in Java
There are mainly 4 memory spaces in Java, the names and functions of these memory are as follows:
- Stack memory space: Saves all object names (more precisely the address of the heap memory space where the reference is saved)
- Heap memory space: Saves the specific property content of each object.
- Global Data area: Saves properties of the static type.
- Global Code Area: Saves all the method definitions.
The memory mechanism of Java