[Java development] Java memory

at first

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.

Method area

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.

Heap area

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. 20200929 Heap 영역 첨부자료1.png

The role of Garbage Collector in the Heap area

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.

image.png

Stack area

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.

image.png

② age is generated and initialized at 10.

image.png

(3) The multiply method is executed and the Stack area is generated.

image.png

(4) The multiply argument is generated and initialized with the passed value.

image.png

(5) value is generated and the result of the operation is substituted.

image.png

(6) return is executed and mutiply execution is completed. And the variables used in multiply are deleted in Stack.

image.png

(7) The execution result of the multiply method is assigned to age.

image.png

⑧ The main method is terminated and the variables used in the main method are deleted.

At the end

That's all for this article.

Thank you for visiting.

Recommended Posts

[Java development] Java memory
Java development training
Java development environment
Java development environment memo
Java development link summary
java development environment construction
[Development] Java framework comparison
Modern Java Development Guide (2018 Edition)
Java Development Basics-Practice ③ Advanced Programming-
Java development environment (Mac, Eclipse)
Java
First Java development in Eclipse
Java Development Basics ~ Exercise (Array) ~
Java
[Eclipse Java] Development environment setting memo
Prepare Java development environment with Atom
Play Framework 2.6 (Java) development environment creation
About the current development environment (Java 8)
Build Java development environment (for Mac)
Measuring instance memory usage in Java
Html5 development with Java using TeaVM
Java development environment (Mac, VS Code)
Install Java development environment on Mac
Java learning (0)
[Java] array
Java protected
Prepare Java development environment with VS Code
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
[Java Spring MVC] Controller for development confirmation
Memory measurement for Java apps using jstat
Examine the memory usage of Java elements
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java development environment construction memo on Mac
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism