everyone. Hello!
It is 254 cm of Dream Hanks.
This time it was mentioned little by little in the java articles so far
I will easily handle the memory that I did not handle.
The summary of Java articles is here.
JVM
Before we talk about memory, we need to know what the JVM is.
JVM is an abbreviation of "** Java Virtual Machine **" and is the one that actually executes Java programs.
The JVM can run Java programs regardless of CPU or operating system.
Some of the JVMs are Class Loader, Execution Engine, Garbage Collector, Runtime Data Area, etc.
Although it is configured, this time only "** Garbage Collector " and " Runtime Data Area **" are dealt with.
Garbage Collector
Garbage Collector is responsible for managing the memory of the JVM.
Garbage Collector removes the memory of objects that are no longer used.
Runtime Data Area
The Runtime Data Area is the space where the actual data is stored when the Java program is executed.
This space is roughly divided into a Method area, a Heap area, a Stack area, a PC Resister area, and a Native Method Stack area.
This time we will only deal with the Method, Heap, and Stack areas.
The Method area is a collection of class information.
Field information, such as class field variable names, data types, access control child information,
Method information such as method name, return data type, arguments, access control,
This area stores static variables, final class variables, etc.
Here static variables are allocated memory when the JVM executes a Java program,
The memory is released when the program ends.
So static variables can be used anywhere as they are kept generated while the program is running.
If you explain the Heap area in detail, it is divided into several areas, but this time I will ignore it.
The Heap area is the area where the data of objects and arrays created by new is stored.
sample
public class ExampleCalss {
public static void main(String[] args) {
Human human = new Human();
}
}
class Human {
int age;
String name;
}
Class field information is stored in the Method area.
When you call the class constructor with new, an instance is created and stored in the Heap area.
At that time, memory is allocated to the field of each instance and it becomes possible to assign a value.
And the variable "** human **" of the Human class stores the address of that instance.
Garbage Collector deletes memory space that no one refers to.
sample
public class ExampleCalss {
public static void main(String[] args) {![20200929 Heap 영역 첨부자료2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/708649/f85e9c28-dbe5-db5d-edd1-6a75a4b54065.png)
Human human = new Human();
human = null;
human = new Human();
}
}
class Human {
int age;
String name;
}
Looking at the above sample, the memory address pointed to by human is null.
And we are letting human create a new instance and refer to it.
That is, no one is referencing the first assigned instance.
Such memory space (first instance) is the target of Garbage Collection.
This area stores data such as local variables, arguments, and return values.
In the Stack area, variables of basic type and their data are stored together,
The reference type (object) stores only the address of the instance, and the actual data is stored in the Heap area.
The Stack area is created individually each time you call a method.
sample
public class ExampleCalss {
public static void main(String[] args) {
int age = 10;
age = mutiply(age, 4);
}
static int mutiply(int num1, int num2) {
int result = num1*num2;
return result;
}
}
Explaining the above cases in order (ignoring args)
(1) The Stack area of the main method is generated.
② age is generated and initialized at 10.
(3) The multiply method is executed and the Stack area is generated.
(4) The multiply argument is generated and initialized with the passed value.
(5) value is generated and the result of the operation is substituted.
(6) return is executed and mutiply execution is completed. And the variables used in multiply are deleted in Stack.
(7) The execution result of the multiply method is assigned to age.
⑧ The main method is terminated and the variables used in the main method are deleted.
That's all for this article.
Thank you for visiting.
Recommended Posts