Memory processing mechanism in Java and final, static, final static summary

Source: Internet
Author: User

Loaded from: http://blog.csdn.net/wqthaha/article/details/20923579

Java programs run on the JVM, you can understand the JVM as a bridge between the Java program and the operating system, the JVM implements the platform independence of Java, this shows the importance of the JVM. So when learning about the Java memory allocation principle, it is important to keep in mind that all this is done in the JVM, which is the basis and premise of the memory allocation principle.

A complete Java program will run with the following memory areas:

Register: JVM internal virtual register, access speed is very fast, the program is not controllable.

Stack: holds the value of a local variable, including: A. The value used to hold the base data type; b. Save an instance of the class, which is a reference (pointer) to the heap object . It can also be used to save the frame when the method is loaded.

heap: used to store dynamically generated data, such as new objects . Note the objects that are created contain only the member variables that belong to them, and do not include member methods. Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

constant Pool: The JVM maintains a constant pool for each loaded type, and the constant pool is an ordered set of constants used by this type. Includes direct constants (basic types, String) and symbolic references to other types, methods, and fields (1). The data in the pool is accessed through the same index as the array. Because a constant pool contains symbolic references to other types, methods, and fields for one type, Chang plays a central role in the dynamic linking of Java. A constant pool exists in the heap .

Code snippet: used to hold the source code read from the hard disk.

Global Data segment: a static member or global variable used to hold a static definition. When allocating this area, the memory is all clear 0, and the resulting variable is initialized to 0.

Represents a memory allocation diagram:

There are several points to note about between Java and memory:

1. A Java file, as long as there is a main entry method, we think this is a Java program, can be compiled and run separately.

2. Whether it is a variable of a normal type or a variable of a reference type (commonly known as an instance), it can be used as a local variable, and they can all appear in the stack. Only the normal type of variable in the stack directly save its corresponding value, and the reference type of the variable is a pointer to the heap area, through this pointer, you can find this instance in the heap corresponding to the object. As a result, a normal type variable occupies only one chunk of memory in the stack, whereas a reference type variable takes up a chunk of memory in the stack area and the heap area.

3. distinguish what an instance is and what an object is. Class a= New Class (), at which point A is called an instance, not a is an object. The instance is in the stack, the object is in the heap, and the operation instance is actually manipulating the object indirectly through the pointer of the instance. Multiple instances can point to the same object.

4. The data in the stack is not synchronized with data destruction in the heap. Once the method ends, the local variables in the stack are destroyed immediately, but the objects in the heap are not necessarily destroyed. Because there may be other variables that point to this object, it is destroyed only if there are no variables in the stack that point to the object in the heap, and it is not destroyed immediately, and can be destroyed when the garbage collection is scanned.

5. The above stacks, heaps, code snippets, data segments, and so on are all relative to the application. Each application corresponds to a unique instance of the JVM, and each JVM instance has its own area of memory that does not affect each other. And these memory areas are shared by all threads. The stacks and heaps mentioned here are concepts on the whole, and these stacks can be subdivided.

6 . The member variables of a class vary in different objects and have their own storage space (the member variable is in the object in the heap). The method of the class is shared by all objects of the class, only one set, the method is pressed into the stack when the object is used, and the method does not consume memory.

Final, static, final static summary

Final if used to represent a data variable, it usually means that the variable is assigned a value once and cannot be changed.

Static if used to represent data variables in an object that is not created, it can be used directly in the object, usually referred to as a static variable, a global variable (no global variable in Java is said)

Final static is used to represent data that can be called directly, without having to declare it in the class that is created, and the variable cannot be modified later. Can often be used with private access restrictions.

Memory processing mechanism in Java and final, static, final static summary

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.