Java Memory Architecture

Source: Internet
Author: User

One, Java memory allocation

1. How many storage areas does Java have? Register Device--within the CPU, developers cannot control the allocation of registers through code and have compilers to manage them.Heap--in Windows, the stack is the data structure to the bottom address extension, is a contiguous area of memory, that is, the address of the top of the stack and the maximum capacity of the stack is the system pre-determined.     -Advantages: Automatically assigned by the system, faster.     -Cons: Not flexible enough for programmers to control. --Storage of basic data types, objects created during development (not during the run).Stack--is the data structure that extends to the high address, which is a discontinuous area of memory.     --There is no stack pointer in the heap, so it is not possible to get support directly from the processor. -the benefit of the heap is that there is great flexibility. As the Java compiler does not need to know how many storage areas to allocate from the heap, it is not necessary to know how long the stored data will survive in the heap.static storage area and constant storage areaStatic storage is used to hold variables of the static type-the constant store is used to hold the value of the constant type (final) type, typically in read-only memory.non-RAM storage--A Stream object that is to be sent to another machine. --persisted objects, stored on disk.2. Java Memory allocation--The underlying data type is allocated directly in the stack space.     --The formal parameter of the method, which is allocated directly in the stack space, when the method call is completed and reclaimed from the stack space.     The reference data type, which needs to be created with new, allocates an address space in the stack space and allocates the class variables of the object in the heap space.     --The reference parameter of the method, which allocates an address space in the stack space and points to the object area of the heap space, when the method call is finished and reclaimed from the stack space. --When the local variable new comes out, allocates space in the stack space and heap space, when the local variable life cycle ends, the stack space is immediately reclaimed, and the heap space area waits for GC to recycle.
--The literal parameter passed in when the method is called, first allocated in the stack space and freed from the stack space after the method call is complete. --string constants are allocated in the data area, this is allocated in space--arrays are allocated both in the stack space and the actual size of the array in the heap space3. Java Memory modelA Java Virtual machine has roughly three logical parts of its governing memory:method Area, Java stack, Java heap。 --Method Areais statically allocated, the compiler binds the variables to a storage location, and the bindings no longer change when they run.     Constant pools, in which named constants, string constants, and static variables in the source code are saved in the method area. --Java Stackis a logical concept, characterized by LIFO.          The space of a stack may be contiguous, or it may be discontinuous. The most typical stack application is a method call, and the Java Virtual machine creates a method frame (frame) each time a method is called, and the corresponding method frame is ejected when exiting.     The data stored in the stack is also determined at run time. --Java HeapAllocation means a memory management model that allocates and reclaims storage space at run time in a random order. The data stored in the heap is often of a size, quantity, and lifetime that cannot be determined at compile time. The memory of the Java object is always allocated in HEAO. Second, Java garbage collection (GC)1. Definition of garbage objects in the JVM runtime environmentAn object is created and placed in the JVM's heap memory, which is reclaimed by the JVM in heap memory when the object is never referenced again. Or when an object cannot arrive through the root set (root set) in the JVM's running space, this object is called a garbage object.2. Heap MemoryCreated when the JVM is started, objects stored in heap memory can be automatically reclaimed by the JVM and cannot be reclaimed by other external means. Heap memory can be divided into two regions: new object area and old object area--new object area can be divided into three districts: Eden, from, to3. The life cycle of objects in the JVMCreate phase--Allocating storage space for objects--Start constructing objects--recursive invocation of the construction method of its superclass--initialization of object instances and initialization of variables--execution of constructor method bodyApplication Phase--Characteristics: The system maintains at least one strong reference to the object; All references to the object are strongly referenced (unless the display is declared as a different reference)non-visual stage--If an object is exhausted and is no longer used in its viewable area, it should be actively set to NULL, that is, Obj=null, which helps the JVM to find the garbage object in time and can reclaim the system resources occupied by the object in a timely manner.          4. The destructor method in Java FinalizeThe Finalize () method is often called a Terminator protected void Finalize () {//Finalization code here} objects are about to be destroyed, sometimes it takes a bit of work to     Some operations are written in the Finalize () method. The Java terminator is called when the object is destroyed. Once the garbage collector is ready to free up storage space consumed by useless objects, it first calls the Finalize () method of those objects before actually reclaiming the object's memory. When the discarded object is destroyed, the application is not known. On most occasions, discarded objects are terminated in the app or are still not destroyed. At the end of the program, not all closure modules are called.5. Can applications interfere with garbage collection? It is impossible to control the garbage collection operation of the JVM in the application code. There are two ways to recycle garbage. The first one is to remove all reference variables that point to an object. This is equivalent to sending a message to the JVM: This object is not. The second is to call the library method System.GC (). The first one is a notification, and calling System.GC () is just a request.     After the JVM receives this message, it does not immediately do garbage collection, but only a few garbage collection algorithms are weighted, so that garbage collection operations are easy to occur, or earlier, or recycled more. It is a requirement to expect the JVM to recycle garbage in time. In fact, there is a need for the opposite: in a certain period of time it is best not to recycle garbage.     It is often desirable to require the fastest real-time systems, especially embedded systems. Java's garbage collection mechanism is serviced for all Java application processes. Therefore, no process can command the garbage collection mechanism to do what, how, and how much.6. Garbage collection Algorithm * Reference CountThe algorithm has not been used in Java virtual machines, mainly circular reference problems, because the count does not record who points to him and cannot discover these interactive self-referencing objects.          --How to count?          When a reference is connected to an object, the object Count plus 1 minus 1 when the reference leaves the scope or is set to null--how is it recycled?          Iterate through the list of objects and release with a count of 0--What's the problem? Circular reference issues * Tagging algorithmThe idea of the tagging algorithm is to start with the objects in the stack and the static store, traverse all references, and tag the live objects. There are two ways to handle a tag:(1) Stop-copyThe so-called stop, is to stop the running program, garbage collection-so-called replication, is to copy live objects to another heap, so that the memory is more compact-the advantage is that when large chunks of memory freed, to facilitate the entire memory redistribution-what is the problem? Stop, the normal operation of the interference program, copy, obviously time-consuming, if the program is relatively stable, less garbage, then each re-replication volume is very large, very uneconomical(2) mark-Clear-that is, to release the object marked as inactive, and also to suspend the program-the advantage is that the program is relatively stable, the garbage is less than the time, faster-what is the problem?          Obviously stopping the program is a problem, and it's only clear that it can also cause a lot of memory fragmentation-why are both algorithms suspending the program? If you don't pause, the tag will be messed up by the running program.

Java Memory Architecture

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.