Java Basics Hardening stacks and stacks in 03:java

Source: Internet
Author: User

1. In the JVM, memory is divided into two parts, stack (stack) and heap (heap), here we understand the stack and heap from the perspective of the JVM's memory management principle, and through these principles to recognize the static methods and static properties in Java problems.

In general, the JVM's memory is divided into two parts: Stack and heap.

Attention:

When the Java program runs, the data is partitioned, heap, stack, method.

Class object is placed in the heap, all class objects are created through the new method, and the stack (stack) creates a reference (memory address) of the class object.

The area of the stack is small, only 1 m, which is characterized by fast access, so the stack is a quick task, such as static variables, static methods, data of basic data types, and object references (reference).

The method area, also called the static zone, holds all class and static variables, and the method area holds the unique elements of the entire program, such as class and static variables.

The method area can be shared by all threads, just like the heap.

The 2.Heap (heap) is the memory data area of the JVM. Heap management is complex and allocates an indefinite amount of memory space each time , specifically to hold an instance of the object . Allocating a certain amount of memory in the heap to hold an object instance is actually just a matter of saving the property value of the object instance, the type of the property and the type tag of the object itself, and not the method of saving the object (the method is instruction, saved in the stack ), Allocating a certain amount of memory in the heap is similar to the serialization of object instances and objects. When an object instance is allocated in the heap, a 4-byte heap memory address is stored in the stack, which locates the object instance in the heap, making it easier to locate the object instance.

stack memory management is allocated sequentially, and fixed-length, there is no memory recovery problem , and Heap is randomly allocated memory, indefinite length, there is memory allocation and recovery problems, so there is another GC process in the JVM, Periodically scans the heap , scans the heap based on the 4-byte object address stored in the stack, locates those objects in the heap, makes some optimizations (such as merging blocks of free memory, etc.), and assumes that the areas that are not scanned in the heap are idle. All refresh(actually clears the useless object that lost the object's address in the stack).

The first thing we need to figure out is what the data is and what the instructions are. Then you need to figure out where the object's methods and object properties are stored separately.


1) The method itself is the operation code part of the instruction, which is stored in the stack ;
2) The internal variables of the method are part of the operand of the instruction, followed by the opcode of the instruction, saved in the stack (in fact, the simple type is saved in the stack , the object type holds the address in the stack, and the value is saved in the heap The above instruction opcode and instruction operand constitute the complete Java instruction.
3) The object instance includes its property values as data and is saved in the data area heap.


Non-static object properties are persisted as part of the object instance in the heap, and the object instance must be accessed through an address pointer saved in the stack. Therefore, the ability to access an object instance and its non-static property values depends entirely on the availability of an object instance's address pointer in the stack.


The difference between a non-static method and a static method:


There is a significant difference between a non-static method and a static method: a non-static method has an implicit incoming parameter, which is given to it by the JVM, regardless of how we write the code, and this implicit argument is the address pointer of the object instance in the stack . Therefore, a non-static method (the instruction code in the stack) can always find its own private data (object property values in the heap). The non-static method must also obtain the implied argument, so the non-static method must first new an object instance to obtain the address pointer in the stack before the call, otherwise the JVM will not be able to pass the implied parameter to the non-static method .


The static method does not have this implicit argument and therefore does not require a new object, which can be called as long as the class file is ClassLoader load into the JVM's stack. Of course, at this point the static method is not accessible to object properties in the heap.


Summarize the process: When a class file is ClassLoader load into the JVM, the method instructions are saved in the stack, where the heap has no data. Then the program technology starts executing the instruction, if it is a static method, executes the instruction code directly, of course, the instruction code is not accessible to the heap data area, if the non-static method, because the implied parameter has no value, will be an error. Therefore, before the non-static method executes, the new object is assigned data in the heap, and the address pointers in the stack are given to non-static methods, so that the program technology executes the instructions in turn, and the instruction code can now access the heap data area.


Static properties and Dynamic properties:


The previously mentioned object instances and the dynamic properties are all stored in the heap, and the heap must be accessed by the instruction (the method of the class) through the address pointers in the stack. It is therefore possible to infer that static properties are stored in the stack, unlike dynamic properties stored in the heap. Because all are in the stack, and the instructions and data in the stack are fixed-length, it is easy to calculate the offset, and therefore, regardless of the instruction (the method of the Class), you can access the static properties of the class. It is also because the static property is stored in the stack, so it has a global property.


In the JVM, static properties are stored in the stack instruction memory area, and dynamic properties are stored in the heap data memory area.

Java Basics Hardening stacks and stacks in 03:java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.