[Reading notes] Java programming ideas Java data storage location

Java data storage location

P46 Page Impressions

First, pre-knowledge

  • Stack is automatically assigned by the system, Java programmer has no direct authority to operate the stack,
  • The heap is shared by all threads of memory areas, each thread stack is exclusive.
  • Heap is a programmer application, when using the new keyword to create an object, the object will be assigned to the heap memory. Since the stack and is assigned automatically by the system, and therefore the application is higher than the efficiency and speed of the new keyword to be used for memory heap.
  • The stack is a contiguous region, and the system determines the size of the stack well, when required stack space is less than the remaining space, the system will automatically assign, stack overflow error will be reported otherwise; and heap space is not a continuous region, If studied courses on operating system can know that the operating system has a linked list of free memory address of record, when the application heap memory, the system will traverse the linked list, find the node address of the first memory space in line with the application space, and then the node is removed from the free list, and then allocated to the program memory space. Details of the allocation process, can go to learn courses on operating system.

Two, Java's six storage location

2.1. Register

Register located inside the CPU, which is the fastest computer hardware in place to deal with, it is the location of the real handlers. Register by the compiler allocates the necessary, and even a compiler, and need to be able to obtain the processing of the CPU through the preemptive scheduling of the operating system, therefore, a very limited time storage registers, developers do not register direct control over, in the program also can not have any operations on the register.

2.2. Stack

Stack generally called stack, generally it refers to a RAM area, after the processing speed register. In the stack, referenced objects are usually stored, when the stack pointer down, you create a new memory, freeing memory and vice versa. When an object is created. The object will be placed in the heap memory space, and the reference to the object will be placed in the stack memory. And Java compiler must know exactly all the "length" of the data stored on the stack and "save
time." In order to recover the stack memory space and also know how much remaining space.

2.3. Heap

Heap memory is the RAM area, it is the real position of the object is stored, when a new object will be stored to heap memory, and the Java compiler does not need to care about how much space is left in the heap, there is a special Java "garbage collector", it looks for all objects created with new, and identify which ones are no longer referenced. Subsequently, it will automatically release occupied by those unused objects in memory, in order to be able to use the new object. This means that we do not have to worry about recovered memory problems. Simply create an object, when you no longer need them, they will automatically leave. This eliminates a common problem in C ++ programming: programmer forgets to release memory "memory", citing the destruction of the object --- an object on the stack, so it was finished using references from the stack destroyed, but the actual object is still stored on the heap, only in the absence of any reference to use when it was only out to destroy the garbage collector.

class StaticTest { 
Static int i = 47; 
}
//现在我们 new两个对象
StaticTest st1 = new StaticTest(); 
StaticTest st2 = new StaticTest();
//st1.i和st2.i都是47 尽管我们制作了两个StaticTest对象,但它们仍然只占据StaticTest.i的一个存储空间。这两个对象都共享同样的i 此时不管是哪个对象执行st1++,另一个对象的i也会加一

2.5. The constant pool

Constant storage. Constant values ​​are often placed directly in the program code. This is safe, because they never change. Some constants are protected, it is possible to consider them into ROM.

2.6 Non-RAM storage

Non-RAM storage. If the data is completely independent from outside a program, the program is not running it can exist outside the control of the program. The two primary example is the "Stream Object" and "fixed object." When streaming object, the object becomes a stream of bytes, generally to be sent to another machine. For fixed objects, objects stored on disk. Even if the program is terminated, they will hold their state unchanged. For these types of data storage, a particularly useful technique something that can exist on the other medium. If necessary, and yet can be restored to normal, the RAM-based object. Java 1.1 provides support for the Lightweight persistence. Future versions of Java might provide more complete solutions.

Three basic types of storage locations

There are so many words for basic data types in the Java programming ideas storage location in

For these primitive types, Java adopted with C and C ++ in the same manner. In other words, instead of creating a variable with the new, but to create "automatic" variable is not referenced. Receiving a specific variable value and placed in the stack, it can be more efficiently accessed.

But I Baidu to find some relevant information, but also said basic data types on the heap. Comb, first of all, the eight basic types of objects can not be seen as basic types of storage location should be divided, when the basic type is created in the class. like this

class Demo{
    int a = 1;//情况1
    public int test(){
        int b = 2;//情况2
    }
}

In case 1, the creation of a global variable. As the object is stored together in a heap, in the case of 2, create a local variable, whenever the program calls the method, the system will establish a method for the method stacks, variables declared in their approach to put methods stack, the method ends when the stack will release method, the corresponding variables declared in the process ended with the destruction of the stack, local variables which can only be effective in the method of reason. Thus, the basic types are not fully stored in the stack area. The reason is because the basics mentioned above, the heap is shared by all threads of memory areas, each thread stack is exclusive . If you will be on the stack within an instance variable, then there is no multiple threads access the same resource objects, which is obviously wrong, so global variables to be created on the heap, not thread safe. But for local variables are created on the stack, each method call to create a stack method, an exclusive area of memory, other threads will not have access to the resources of the thread to create will also reduce the GC on the stack pressure, with the end of the process, the corresponding method to eliminate the stack memory, the memory occupied by local variables such naturally disappear, so local variables are thread-safe.

Guess you like

Origin www.cnblogs.com/blackmlik/p/12203771.html