In any programming language, whether it is a basic type or a reference type, regardless of its scope, it must be allocated a certain amount of memory space, the Java language is no exception, Java data types can be divided into two types: the basic type (the variable holds the value of the data itself) and the reference type (is a reference to an object, Instead of the object itself), the base types are Boolean, float, double, int, long, short, Byte, and Char, and in the Java programming language all but the base type are reference types such as: class type, array type, and so on.
In computer memory mainly from four places: Heap segment (heap area), stack segment (stack), Codesegment (code area), data segment Different places store different data: The heap area is mainly stored in the Java program to create all the reference types are placed in it, the main storage of Java program to run the local variables required, the parameters of the method, the object reference and the results of intermediate operations and other data; Code area main Java code The data area mainly stores static variables and global variables, and the following examples are given to discuss their specific mechanisms.
class Student { private String name; private int age; Public Student (String name, int.) { this.name = name; this.age = age; } } Public class Test { static int i = ten; Public static void Main (string[] args) { Student S1 = new Student ("Feng", +); } } |
When the program is running, its computer memory is distributed roughly as follows:
Objects internally represent the Java Virtual Machine specification and do not specify how it is represented in the heap. The internal representation of an object directly affects the design of the heap area and the design of the garbage collector (GC).
There are two specific ways that Java is represented in a heap:
Divide the heap into two parts: a handle pool, an object pool, and a representation as shown:
The object pointer points directly to a set of data, which includes the object instance data and a pointer to the data in the method area, as shown in:
Summary: through the analysis of the object memory allocation, so that the reader to Java the bottom of a more rational understanding, so as to further grasp the basic knowledge of Java. After deeply understanding the allocation of Java memory, we can lay a solid foundation for writing high-quality programs later, and use this idea to analyze the memory allocation of other object-oriented languages.
Memory Allocations in Java