Introduction to MVJ and introduction to Lala shuxi mv

Source: Internet
Author: User
Tags xms

Introduction to MVJ and introduction to Lala shuxi mv

About java JVM, some performance optimizations will involve databases and some caches will be placed in the memory after the project grows. There are also some JMS message propagation and so on. The advanced knowledge requires the support of JVM Memory Model knowledge. Answer your questions.

1. Talk about memory and JVM memory and Tomcat memory.

Memory is what we often call the memory on the computer. For example, my mobile phone uses 1 GB memory and my desktop computer uses 4 GB memory.

JVM memory. We know that the JVM memory is divided into two parts: heap and non-heap. Then, the JVM memory is the memory and the memory involved in the java language. The maximum heap memory can be allocated to 1/4 of the physical memory, and the maximum non-heap memory can also be allocated to 1/4 of the physical memory, so the maximum JVM memory is 1/2 of the memory. (The configuration parameters are described in detail in the supplementary section)

Tomcat isContainerContainers load classes according to rules and run classes. Tomcat memory is essentially JVM memory. Our common java. lang. OutOfMemoryError and other memory overflow problems are solved by adjusting the JVM memory.

Additional knowledge:

The concepts of containers, components, Tomcat, Servlet, and JSP are differentiated.Let's talk about components firstA component is a reusable code set that can complete a function and provide a number of interfaces that use this function. The expression is common (library \ Package). Components organize some classes and interfaces to expose one or more interfaces for external calls. For example, if the JAR package used for JDBC development includes the jdbc api, we develop JDBC to use the APIs in these JAR packages, that is, functions provided by the JDBC component are used. These functions are presented through interface declaration.Relationship between containers and components, Container (specification) + component (specification) = program. A container provides a runtime environment, including transaction support and logs. A component is a module that provides certain functions. Middleware is a type of basic software and belongs to reusable software. Middleware is in the middle of the operating system software and the user's application software. Middleware is built on the operating system, network, and database, and the lower layer of the application software. Its role is to provide an environment for running and developing the application software on its upper layer, it helps users develop and integrate complex application software flexibly and efficiently.

For example, if you use the J2EE architecture, you must use a Web container if you use Servlet technology. If you use EJB, you must use an EJB container. With servlet, you only need to simply implement the doPost () and other interfaces. The other interfaces are processed by containers, and the other interfaces are processed by middleware servers. You operate the database and have middleware jdbc to help you deal with it. JDBC is only a component of the J2EE standard system. A container is used to provide the runtime environment. It is used to "Install" things and depend on this container to provide components of the runtime environment.Tomcat is a web Container, but it is used to run servlet and jsp.

Additional knowledge:
Take a look at SUN's official documents: "A Java virtual machine has a heap. The heap is the runtime data area, and the memory of all class instances and arrays is allocated from this place. The heap is created when the Java Virtual Machine is started ." "Memory outside of the heap in JVM is called Non-heap memory )". JVM manages two types of memory: heap and non-heap. In short, heap is the memory available for Java code and is reserved for developers. Non-heap is reserved for JVM, therefore, the method area, JVM internal processing or optimization of the required memory (such as the code cache after JIT compilation), each class structure (such as the runtime data pool, field and method data) the methods and constructor code are all in non-heap memory.

Heap memory
Concept: heap memory allocation the initial memory allocated by JVM is specified by-Xms. The default value is 1/64 of the physical memory. The maximum memory allocated by JVM is specified by-Xmx. The default value is 1/4 of the physical memory.
Features: by default, when the free heap memory is less than 40%, the JVM will increase the heap size until the maximum limit of-Xmx; when the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-Xms. Therefore, the server generally sets-Xms and-Xmx to be equal to each other to avoid adjusting the heap size after each GC.
Non-heap memory
Non-heap memory allocation JVM uses-XX: PermSize to set the non-heap memory initial value. The default value is 1/64 of the physical memory. The maximum non-heap memory size is set by XX: MaxPermSize, the default value is 1/4 of the physical memory.
Features: JVM memory limit (maximum) First, JVM memory is limited to the actual maximum physical memory. If the physical memory is infinitely large, the maximum JVM memory has a great relationship with the operating system. Simply put, although the 32-bit processor has a controllable memory space of 4 GB, the specific operating system will impose a limit, this limit is generally 2 GB-3 GB (1.5 GB-2 GB in Windows and 2 GB-3 GB in Linux ), the 64-bit and above processors will not be limited.

2. Explain the JVM memory policy in detail, and then analyze the GC's management of these territories.

In the JVM, the initial memory allocated by the JVM is specified by-Xms. The default value is 1/64 of the physical memory. The maximum memory allocated by the JVM is determined by-Xmx. The default value is 1/4 of the physical memory. By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-Xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-Xms. Therefore, the server generally sets-Xms and-Xmx to be equal to each other to avoid adjusting the heap size after each GC.The object heap memory is recycled by the automatic memory management system called the garbage collector..

Heap: Young Generation includes Eden and two consumer vor spaces. The Eden area is used to store new-born objects, and the Survivor area is used to store objects that survive each garbage collection. Old Generation is used to store long-lived objects in an application.

**************************************** **************************************** **************************************** **************************************** ****************

Non-heap: JVM uses-XX: PermSize to set the non-heap memory initial value. The default value is 1/64 of the physical memory. The maximum non-heap memory size is set by XX: MaxPermSize, the default value is 1/4 of the physical memory. Non-heap memory consists of Permanent Generation and Code Cache. The English meaning of Permanent is Permanent. Permanent Generation is used to save the refective data of the virtual machine. It mainly stores the static Class-level objects loaded, such as class, method, and field. Insufficient permanent generation space will lead to full GC (GC is divided into the young generation GC and the old generation GC, and full GC is the GC of the old generation ). Code Cache for compiling and saving local Code.

To talk about the JVM memory allocation process, first place the object in the Eden area. If the Eden space is sufficient, end the memory allocation. If not, recycle the Eden area and release the inactive objects in the Eden area, if you still cannot store objects, place them in the same vor area. The same vor area is used as the intermediate swap area between Eden and Old. When the old area has enough space, the region of the same vor space will be moved to the Old area. When the space in the Old area is insufficient, GC will perform full GC in the Old area. After the corresponding GC, there is still not enough space to put down the objects in the same VOR and Old areas, so an error occurs in the out of memory.

Next we will focus on the composition of non-heap. We will heap to non-heap based on our memory logic, and then carefully separate the non-heap memory composition for understanding, as shown below:

First, the program counter.

Keywords:Similar to program counters in composition principle (command indicator VS bytecode line number indicator); thread private; no exception area;

Detailed description:The Program Counter Register (Program Counter) is a small memory space. Its function can be seen as the row number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, when the bytecode interpreter is working, it selects the next bytecode instruction to be executed by changing the counter value, this counter is required for basic functions such as branch, loop, jump, exception handling, and thread recovery. Java Virtual Machine multithreading is implemented by switching threads in turn and allocating the processor execution time. At any definite moment, a single processor (for a multi-core processor, It is a kernel) only the commands in one thread are executed. Therefore, in order for the thread to be switched back to the correct execution position, each thread requires an independent program counter. The counters between each thread do not affect each other and are stored independently, we call this type of memory region as "thread-private" memory. If the thread is executing a Java method, this counter records the address of the Virtual Machine bytecode instruction being executed; if the Natvie method is being executed, this counter value is null (Undefined ). This memory region is the only region that does not specify any OutOfMemoryError conditions in the Java Virtual Machine specification.

Second, stack (also known as Java Virtual Machine stack and Java stack)

Keywords:The thread is private; the local variable table is stored (Basic Data Type + application data type); the local variable table is allocated during the compilation period; the coders care about it and heap; and An OutOfMemoryError occurs;

Detailed description:Like program counters, Java Virtual Machine stack (Java Virtual Machine Stacks) is also proprietary to threads, and its lifecycle is the same as that of threads. The VM stack describes the Memory Model of Java method execution: when each method is executed, a stack frame is created to store information such as the local variable table, Operation stack, dynamic link, and method exit. The process of calling each method until execution is complete corresponds to the process of a stack frame in the VM Stack from the inbound stack to the outbound stack. Java memory is often divided into Heap memory and Stack memory. This method is rough, and the Division of Java memory areas is actually far more complex than this. The popularity of this partitioning method only shows that the heap memory area that most programmers are most concerned about and closely related to object memory allocation is the two blocks, and the "stack" refers to the Virtual Machine stack., or the local variable table in the VM stack. The local variable table stores various basic data types (boolean, byte, char, short, int, float, long, and double) and object references (reference type, it is not the same as the object itself. According to different virtual machine implementations, it may be a reference pointer pointing to the starting address of the object, or it may point to a handle representing the object or other locations related to the object) and returnAddress (pointing to the address of a bytecode instruction ). Data of the 64-bit long and double types occupy two local variable spaces (slots), and the remaining data types only occupy one space. The memory space required by the local variable table is allocated during compilation (so local variables in Java must be manually initialized). When entering a method, the size of the local variable space allocated to the frame in this method is completely determined, and the local variable table size will not be changed during the method running. In the Java Virtual Machine specification, two exception conditions are specified for this region: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, an StackOverflowError exception will be thrown; if the Virtual Machine stack can be dynamically expanded (currently most Java virtual machines can be dynamically extended, but the Java Virtual Machine specification also allows a fixed-length Virtual Machine stack ), an OutOfMemoryError error is thrown when the extended memory cannot be applied.

Third, local method Stack

Keywords: Virtual Machine stack execution Java method VS local method stack execution Virtual Machine Native method;

Detailed description: THE Native Method Stacks plays a very similar role with the Virtual Machine stack. The difference is that the virtual machine stack performs Java (also known as bytecode) services for virtual machines, the local method stack is the Native method service used by virtual machines. The language, usage, and data structure of the methods in the local method Stack are not mandatory in the virtual machine specification. Therefore, the specific virtual machine can implement it freely. Even some virtual machines (such as Sun HotSpot virtual machines) directly combine the local method stack with the Virtual Machine stack. Like the Virtual Machine stack, StackOverflowError and OutOfMemoryError are thrown in the local method stack area.

Fourth, heap

Keywords: coders favor stacks (heap thread sharing) (stack thread private); heap is new;

For most applications, Java heap is the largest memory managed by Java virtual machines. Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this memory area is to store object instances, where almost all object instances allocate memory. This is described in the Java Virtual Machine specification: All object instances and arrays must be allocated on the stack. Java heap is the main area for managing the garbage collector. From the perspective of memory collection, since the collectors are basically using generational collection algorithms, the Java heap can also be subdivided into the new generation and old generation; the details include Eden space, From other vor space, and To another vor space. From the perspective of memory allocation, the Java heap shared by threads may be divided into private allocation buffers (Thread LocalAllocation Buffer, TLAB) for multiple threads ). However, no matter how it is divided, it is irrelevant to the storage content. Regardless of the region, the storage is still an object instance. The purpose of further division is to better recycle the memory, or allocate memory faster. According to the Java Virtual Machine specification, Java heap can be in physically discontinuous memory space, as long as the logic is continuous, just like our disk space. In implementation, it can be either fixed or scalable, however, the current mainstream virtual machines are implemented according to scalability (through-Xmx and-Xms control ). If the heap does not have memory for instance allocation and the heap cannot be extended, an OutOfMemoryError will be thrown.

Fifth, Method Area

Keywords: thread sharing; stores constant static variable class information;

Description: The method area is the same as the Java heap. It is the memory area shared by each thread, it is used to store data such as class information, constants, static variables, and Code Compiled by the real-time compiler loaded by virtual machines. Although the Java Virtual Machine specification describes the method area as a logical part of the Heap, it has an alias called Non-Heap (Non-Heap), which should be distinguished from the Java Heap. The Java virtual machine specification imposes very loose restrictions on this region. In addition to the same Java heap, it does not require continuous memory and can be fixed or scalable. In addition, it can also choose not to implement garbage collection. Relatively speaking, the garbage collection behavior rarely occurs in this area, but it does not mean that the data enters the method area, just as the permanent name is "permanent. The memory recovery target for this region is mainly for the collection of constant pools and the uninstallation of types. In general, the "score" for the collection in this region is hard to satisfy, especially for the type of uninstallation, the condition is quite harsh, but the recovery in this part of the area is indeed necessary. In Sun's BUG list, several serious bugs have been reported because the earlier versions of the HotSpot virtual machine did not completely recycle the region, leading to memory leakage. According to Java Virtual Machine specifications, when the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception will be thrown.

Sixth, runtime frequent pool

Keywords:

The Runtime Constant Pool is part of the method area. In addition to the description of the version, field, method, and interface of the Class, the Class file also contains the Constant Pool Table ), it is used to store various types of literal and symbolic references generated during the compilation period. This part of content will be stored in the runtime pool of the method area after the class is loaded. Java virtual machines have strict rules on the format of each part of the Class file (also including the constant pool), and each byte must be used to store any data in compliance with the standard requirements, this will be recognized, loaded, and executed by the virtual machine. However, Java Virtual Machine specifications do not have any detailed requirements for runtime frequent pools. virtual machines implemented by different providers can implement this memory area as needed. However, in general, apart from saving the symbolic references described in the Class file, the translated direct references are also stored in the runtime pool. Another important feature of the runtime constant pool relative to the Class file constant pool is that it is dynamic. Java does not require constants to be generated only during compilation, that is to say, it is not the content of the constant pool preset into the Class file that can enter the constant pool for running in the method area. During running, new constants may also be placed in the pool, the intern () method of the String class is used by developers. Since the runtime constant pool is part of the method area, it is naturally limited by the method Area Memory. When the constant Pool cannot be applied to the memory again, an OutOfMemoryError error is thrown.

7. Direct Memory

Description:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Direct Memory is not part of the data zone during virtual machine running hours, nor is it the Memory area defined in Java Virtual Machine specifications. However, this part of Memory is also frequently used, it may also cause an OutOfMemoryError exception, so we will explain it here. The NIO (New Input/Output) class is added to JDK 1.4, and an I/O Method Based on Channel and Buffer is introduced, it can use the Native function library to directly allocate off-heap memory, and then use a DirectByteBuffer object stored in the Java heap as a reference to this memory. This can significantly improve the performance in some scenarios, because it avoids repeated data replication in the Java and Native stacks.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Obviously, the distribution of direct memory on the local machine is not limited by the Java heap size. However, since it is memory, it is certainly still subject to the total memory on the local machine (including RAM, SWAP, or paging files) and the limitation of the addressing space of the processor. When configuring virtual machine parameters, the server administrator usually sets parameters such as-Xmx based on the actual memory, but often ignores the Direct Memory, this causes an OutOfMemoryError to occur when dynamic expansion occurs because the total amount of each memory area exceeds the physical memory limit (including physical and operating system-level restrictions. We have already seen the Logical Memory Model. How can we access it when we create an object? In Java, how does object access work? Object Access is everywhere in the Java language and is the most common program behavior, but even the simplest access, this Code also involves the association between the three most important memory areas, namely the Java stack, Java heap, and method zone:

 

Object obj = new Object ();

 

 

 

Assuming that the code appears in the method body, the semantics of the "Object obj" part will be reflected in the local variable table on the Java stack, and will appear as a reference type data. The semantics of "new Object ()"

 

Will be reflected in the Java heap, forming a block that stores all Instance Data values of the Object type (Instance Data

 

Structured memory, according to the specific type and Virtual Machine implementation Object Memory Layout

 

The Memory length varies depending on the Object Memory Layout. In addition

 

It must also contain the location where data of this object type (such as the object type, parent class, implemented interface, and method) can be found.

 

Address information. These types of data are stored in the method area.

 

Because the reference type only specifies a reference pointing to an object in the Java Virtual Machine specification

 

Define the method in which the reference should be located and the specific location of the object in the Java heap. Therefore

 

Different virtual machines implement different object access methods. There are two mainstream access methods: handle and direct access.

 

Pointer.

 

If you use the handle access method, the Java heap will divide a piece of memory as the handle pool.

 

Is the handle address of the object, and the handle contains the corresponding

 

Shows the specific address information.

 

 

 

Http://www.cnblogs.com/dingyingsi/p/3760447.html

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.