An example of analyzing heap memory and stack memory in Java

When running a Java program, you may encounter the situation that the program memory overflows and an error is reported, as follows

package 面向对象设计模式;
import 面向对象设计模式.Person;
public class Test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Person p = new Person();
		p.eat();
	}

}
package 面向对象设计模式;

public class Person {
    
    
	public String name;
	public int age;
	public Person() {
    
    
		System.out.println("I......");
	}
	public void eat() {
    
    
		this.sleep();
		System.out.println("This is A....");
	}
	public void sleep() {
    
    
		this.eat();
		System.out.println("This is B...");
	}
}

insert image description here

This is due to stack overflow.

First of all, in Java, there are two types of memory:

  • Heap memory: used to store objects and arrays in Java, which is characterized by first-in, first-out, last-in, last-out (with both entry and exit). It allocates memory dynamically at runtime, but also makes access slower.
  • Stack memory: mainly used to store methods or local variables, etc., for executing programs. For example: variables of primitive types and reference variables of objects. Its characteristics are: FIFO, LIFO (only one port). Its access speed is faster than the heap, second only to the register, and the stack data can be shared, but it also lacks flexibility.

Next, analyze why the stack memory overflows in the example

  • The p object in the Test class is stored in the heap. First, public Person() is executed, and then the eat() method in the Person class is called.
  • The variables and methods in the Person class are stored in the stack. When the program runs to the eat() method, a method object of the void class will be created in the stack to refer to the eat() method (push into the stack), and then find the not stored
{
    
    
		this.sleep();
		System.out.println("This is A....");
	}

This method, if not, put the method on the stack, and make eat() point to its content, if there is already the method content, directly make eat() point to its content, which is equivalent to pointing to the address where the method content is stored.
When running to the eat() method, other methods sleep() in the Person class are called due to the this keyword, so a method object of the void class will be created in the stack to refer to the sleep() method (pushed onto the stack), and then Find out if there is any storage in the stack

{
    
    
		this.eat();
		System.out.println("This is B...");
	}

This method, if not, put the method on the stack, and let sleep() point to its content, if there is already the method content, directly make sleep() point to its content, which is equivalent to pointing to the address where the method content is stored.
When running the sleep() method, the this keyword was also encountered, and the other method eat() in the Person class was called, so the eat() and sleep() methods were continuously created in the stack, and both methods could not end because , so the stack cannot be popped, causing the stack to overflow and the program to report an error.

Guess you like

Origin blog.csdn.net/m0_55887872/article/details/124305246