The introduction

Recently when I was learning Java basics, the teacher talked about object orientation, memory is a very basic part of the knowledge, but it is also a very, very important part. When you understand how code is distributed in memory, you can write and understand code with less effort. The first time actually I did not see too understand, the second time to see, there is a different understanding. Review the past and learn the new! \

Basic concept?

Each Running Java program generates a Java process, and each Java process may contain one or more threads. Each Java process has a unique JVM instance, each JVM instance has a unique heap, and each thread has its own private stack. All instances of classes (that is, objects) or arrays (arrays themselves, not references) created by a process are placed in the heap and shared by all threads of that process.

Allocating heap memory in Java is automatically initialized, that is, when allocating memory for an object, variables in that object are initialized. Although all the objects in the Java storage space is allocated in the heap, but the object reference is allocated in the stack, that is to say, when establishing an object on the heap and stack allocated memory, in the heap memory allocated to store the objects that were created actual itself, and the memory allocated in the stack is stored only refer to the heap object references. When the local variable new comes out, space is allocated between the stack space and the heap space. When the local variable life cycle ends, the stack space is immediately reclaimed and the heap space area is waiting for GC collection.


The stack: In general, basic data types allocate space directly on the stack. Local variables (variables defined in a method snippet) also allocate space directly on the stack and are reclaimed by the JVM when the method in which they are located completes execution. There are also reference data types. That we say normally need to use the new keyword to create the corresponding reference object as well as in the stack space, at this point, the JVM to object references in the stack space allocated a address space (the equivalent of a house number, through the house number). Then you will find your house, to the reference object in the heap space distribution of a space, Address references in stack space refer to object areas in heap space (address by house number);

Heap: generally used to store the data out of the keyword new.

What is the difference between a heap and a stack in Java? :

  1. The stack and heap are both places Java uses to store data in Ram. Unlike C++, Java automatically manages stacks and heaps, and programmers cannot set up stacks or heaps directly.
  2. The advantage of the stack is that access is faster than the heap, second only to registers located directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be determined, which lacks flexibility. In addition, stack data can be shared. The advantage of the heap is that the size of memory can be allocated dynamically, the lifetime does not have to be told to the compiler in advance, and the Java garbage collector automatically collects the data that is no longer used. The downside is that access is slow because memory is allocated dynamically at run time.

Next, please follow me through a program to intuitively feel the memory allocation and its changes when the program runs. \

Analysis by example?

What about code for example?

Public class Test {
	public static void main (String args[] { Test test = new Test ();
		int date = 9;
		BirthDate d1 = new BirthDate (7.7.1970);
		BirthDate d2 = new BirthDate (1.1.2000);
		test.Change1 (date);
		test.Change2 (d1);
		test.Change3 (d2);
		System.out.println ( "date = " + date);
		d1.display ();
		d2.display();
	}
	public void change1 (int i) {i = 1234; }public void change2 (BirthDate b) {
		b = new BirthDate (22.2.2019);
	}
	public void change3 (BirthDate b) {
		b.setDay (22); }}Copy the code

(Is this my bad performance?)

There are two parts of the code. New Test appears. When you see a new object, you need to think about the development of stack and heap. Int date=9, which assigns a value to an int date variable ❓

? Eight of the four classes in Java are basic data types that occupy only a chunk of memory on the stack. In addition to these four categories, the rest are what we call “reference types,” and reference types take up two pieces of memory, one stack and one heap. As shown in figure.

    

Analysis of theNext is memory allocation and how it changes, right? Which corresponds to how memory is allocated during each line of code execution.

First, let’s look at the memory distribution. Date has space in the stack for the data 9. Test.change1 (date) : I (1234) : I (1234) : I (1234) : I (1234) : I (1234) : I (1234) So the memory block in the stack where date 9 was stored is marked with a variable name I. After calling change1, data 9 becomes 1234. Because I is a local variable, it disappears from the stack after the method is called.

Further down the stack, two local variables, d1 and d2, appear. Make sure that they have two Spaces on the stack to store their respective addresses. The change2 method is similar to the previous new method, except that after it is called, the local variable on the stack disappears immediately, and the new object in the heap loses reference to it. It won’t be long before the magic Java garbage collector takes it away.

Let’s seechange3Method,change3Call theBirthDateThe methods inside the object have changedDayThe data for22.

(Note: the dashed line is the concave that will disappear)

Can I say something?

If you haven’t noticed a little secret from the memory analysis above, local variables do have a place in stack memory, but after executing their corresponding methods, they disappear, and the heap objects created by them are taken away by the garbage collector shortly after. However, by calling internal methods on objects like Change3, the changes to the heap are permanent until the main method is finished executing.