Java Programming thought study note _1

Source: Internet
Author: User

Where the data is stored in the 1.Java object:

There are five different places where data can be stored.

1) register. The fastest, because it is located inside the processor, registers are allocated on demand and cannot be directly controlled.

2) stack. Located in Universal RAM, the stack pointer allows direct support from the processor. The stack pointer moves down, allocates new memory, and moves up, freeing those memory. The Java system must know the exact life cycle of all items stored within the stack.

3) heap. The compiler does not need to know how long the stored data will live in the heap.

4) constant storage. Usually placed directly inside the code.

5) non-RAM storage, if the data is completely alive and out of the program, then it can be free from any control of the program, when the program is not running, but also exist, the most basic example is the stream object and persisted object.

Storage of 2.Java data:

According to the Java Virtual Machine specification, the runtime data area typically includes these parts: program Counter Register, Java stack (VM stack), local method Stack (Native), method area ( Method area), heap.

1) Program counter, the program counter is a small area, its role can be seen as the current thread execution of the byte code indicator line number. In the virtual machine model, the bytecode indicator specifies the next instruction to be executed by changing the value of the program counter. The basic functions of branching, looping, etc. are dependent on the program counter to complete. In multi-threading, it is necessary to pass the program counter to see where the current thread is executing, and to start from its original position after the next execution. The program counter is thread-private. If the virtual machine is executing a Java method, the counter specifies the address of the bytecode directive, and if a local method is being executed, the counter specifies that the undefined is empty.

2) The Java stack is also known as the virtual machine stack, and the program counter is also thread-private, life cycle and thread the same, the virtual machine stack describes the Java method execution of the memory model: Each method is executed when a stack frame is used to store local variables table, Operation Stack, dynamic link, method exports and other information. The process by which each method is called corresponds to a stack frame in the virtual machine stack from the stack to the stack. The virtual machine stack is also divided into local variable tables (the local variables to be used by the storage method), the operand stack (used in the data structure to perform the calculation), the reference to the run-time pool, the method return address, and the additional message. the Java stack is the memory model that the Java method executes.

3) Local method stack: the Java stack is for executing Java method Services, and the local method stack is for performing local method services.

4) Heap: Used in Java to store the object itself (including the object's member variables, arrays), Java's automatic garbage collection mechanism is mainly to reclaim this part of the space. The heap is shared by all threads, and the Java Virtual machine has only one heap.

5) Method Area: The method area and the heap are also shared by the thread, in the method area, the information of each class is stored (including the name of the class, method information, field information), static variables, constants and compiler compiled code, and so on. An important part of the method area is called a constant pool, and when classes and interfaces are loaded into the JVM, the Chang is loaded. New constants can be placed in a constant pool during run time.

3.Java Automatic garbage collection mechanism: End processing and garbage collection:

1) The Finalize method is detailed:

  The Finalize method is the protected method of object, which can override this method implementation and the GC calls this method before the object is reclaimed. The Finalize method is not recommended for cleanup of non-memory resources, but is recommended for: ① Cleanup of local objects ( objects created by JNI); ② as a supplement to ensure that some non-memory resources, such as sockets, files, and so on, are freed by explicitly invoking other resource-freeing methods in the Finalize method.

First, the finalize process is roughly described: When the object becomes (GC Roots) Unreachable, the GC determines whether the object overrides the Finalize method, if it is not overwritten, It is recycled directly. Otherwise, if the object does not perform a Finalize method, it is placed in the F-queue queue, and a low-priority thread executes the Finalize method of the object in that queue. After the Finalize method is executed, the GC will again determine if the object is reachable, and if not reachable, the object "Resurrection". (That is, when the object becomes a GC roots unreachable, the object executes the Finalize method, at which point the object is resurrected (but the internal tag shows that the Finalize method has been called), and if the GC roots is still unreachable, The internal tag shows that the Finalize method has been called and is recycled directly. You can manually display the close resource or the Resurrection object inside the Finalize method.

In addition, because of the uncertainty of the Finalize method invocation (in fact, the garbage collection thread has a lower priority), it is very likely that garbage is present but not reclaimed in time.

2) How the garbage collector works:

Garbage collection mechanisms for other systems:

1) Reference counting method

  Each object contains a reference counter, and when a reference is connected to an object, the reference count is +1, and the reference count-1 when the reference leaves the scope or is set to null. The garbage collector iterates through the list of all objects, and if it finds that an object count value becomes 0, The object is immediately disposed of. However, there is a flaw in this approach, that is, when there is a circular call between objects, the object should be recycled, but the counter is not 0. The following code shows the loop call:

 Public classTest2 { Public Static voidMain (string[] args) {Dog d=NewDog (); Cat C=NewCat (); D.cat=C; C.dog=D;
D=null;
C=null;
}}classDog { Publiccat Cat;}classCat { Publicdog Dog;}

  2) optimized garbage collection:

  For any living object, it must be traceable to its reference that survives the stack or static storage. Thus, if all references are traversed from the stack and from the static storage area, a live object can be found. For each reference that is discovered, you must trace the object it references, and then the reference that this object contains. Know that the networks that originate from the references to stacks and static stores are all accessed. Objects that are not accessed are objects that need to be processed by the garbage collection mechanism.

  3) Java garbage collection mechanism:

Stop replication: pauses the program's run, then copies all the surviving objects from the current heap to the other heap, all of which are not replicated. When objects are copied to the new heap, they are next to each other, so the new heap can be arranged in a compact form. But in Java, Large objects are still not copied. The blocks that contain small objects are copied and collated.

  tag sweep: from the stack and static storage, traverse all references to find all the surviving objects, set a marker whenever a surviving object is found, and finally, when all the markup is done, Objects that are not marked will be freed. So the remaining heap space is discontinuous. The Java virtual machine will be monitored: if all the objects are stable and the garbage collector is inefficient, switch to "tag sweep" mode, the same virtual opportunity with the results of the comprehensive tag sweep, if there is a lot of debris in the heap space, Switch back to stop replication. This is the "adaptive" Technology of the garbage collection mechanism of the Java Virtual machine.

Java Programming thought study note _1

Related Article

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.