Java Virtual machine Performance monitoring and tuning combat

Source: Internet
Author: User

The memory structure of the Java Virtual machine, as distinct from the Java memory model, which focuses on multithreading

  

But before we do that, we should consider: Why is the memory structure of the JVM divided?

I think the main is based on the different data update frequency, access speed requirements, garbage collection management of the five major memory areas of the JVM--PC register, JVM Stack, Native method Stack, Java Heap, method area, shared area

The following is a detailed explanation of these five memory areas:

1.PC Registers

The PC Register (program Counter Register), which is a small memory space, can be seen as the line number indicator of the byte code executed by the current thread. In the virtual machine conceptual model, the bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, need to rely on this counter to complete.

Because the JVM's multithreading is achieved by rotating the allocation of CPU execution time, at a certain point, a cpu/kernel executes only one thread of instruction. In order for the thread to switch back to the correct execution location, each thread needs to have a private, independent PC register, and each thread does not affect each other, we call this memory area "thread-private" memory.
If the thread is currently executing a Java method, the PC register records the address of the executing virtual machine bytecode instruction
If the thread is executing the native method, its value is empty (undefined)

2.JVM Stack

Like the PC registers, the JVM stack is also thread-private. It has the same life cycle as the thread. The JVM stack describes the memory model that the Java method executes: Each method creates a frame to store the local variable table, the operand stack, the dynamic link, the method exit, and so on. Each method from the call to the completion of the process, corresponding to a frame in the JVM stack into the stack into the stack process

Often someone divides Java memory into heap memory (heap) and stack memory (stack), which is coarser, and the division of Java memory areas is far more complex. The "stack" refers to the current JVM stack, or the local variable table portion of the JVM stack (directly over the stack and frame).

Stackoverflowerror is thrown when a thread requests an allocated stack capacity that exceeds the maximum allowable capacity of the JVM;

If the JVM can be dynamically extended and the action of the extension has been tried, but the current cannot request enough memory to complete the extension, or if there is not enough memory to create the corresponding JVM Stack when creating a new thread, OutOfMemoryError will be thrown.
Stackoverflowerror indicates request > Stack.max
OutOfMemoryError indicates request > assignable memory

3. Local Method Stack

The local method stack is very similar to the JVM stack, except that the JVM stack executes the Java method service for the virtual machine, and the local method stack is the native method service used for the virtual machine.

In the virtual machine specification, the methods used in the local method stack are not mandatory in terms of language, usage and data structure, and are freely chosen by implementation. Even some virtual machines, such as hotspots, directly combine the local method stack with the JVM stack. As with JVM stacks, stackoverflowerror and OutOfMemoryError exceptions are also thrown

def default_key_func (Key, Key_prefix, version):
  
"""
  
Default function to generate keys.
  
Constructs the key used by all other methods. By default it prepends
  
The ' Key_prefix '. KEY_www.taohuaqing178.com FUNCTION can be used to specify an alternate
  
function with custom key making behavior.
  
"""
  
Return '%s:%s:%s '% (key_prefix,www.dongfan178.com/version, key)
  
def get_key_func (Key_func):
  
"""
  
function to decide which key function to use.
  
Defaults to ' Default_key_func '.
  
"""
  
If Key_func is not None:
  
If callable (Key_func):
  
Return Key_func
  
Else
  
Return import_string (Www.fencaiyule.cn/key_func)
  
Return Default_key_func
  
Copy Code
  
Memory:
  
Description: In the memory version, you must set a value that is unique, which saves the content to a variable in memory
  
  
CACHES = {
  
' Default ': {
  
' Backend ': ' Django.core.cache.backends.locmem.LocMemCache ',
  
' Location ': ' www.chaoyueyule.com unique-snowflake ', #这边必须设置一个值, this value is unique
  
}
  
#其他的配置和开发调试版本一样
  
}
  
File: Description: This cache saves the content to a file

CACHES = {
  
' Default ': {
  
' Backend ': ' Django.core.cache.backends.filebased.FileBasedCache ',
  
' Location ': '/var/tmp/django_cache ', #缓存存放的路径
  
}
  
#其他的配置和开发调试版本一样
  
}
  

4.Java Heap

For most applications, the Java heap (Java heap) is the largest piece of memory managed by the JVM, a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area is to hold object instances where almost all object instances allocate memory.

The Java heap is the primary area of GC management. The Java heap is subdivided into the new generation and the old age, the more fine Eden Space, the from Survivor space, to the survivor space, and so on, since the collector basically uses the Generational collection algorithm. However, regardless of the partitioning, it has nothing to do with the content, no matter which area is storing the object instance.

When implemented, it can be either fixed-size or extended, but mainstream virtual machines are implemented in a scalable way (-XMX and-XMS). If there is no memory in the heap to complete the instance assignment, and the heap can no longer be expanded, OutOfMemoryError is thrown

5. Method area

Like the Java heap, the method area is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Although the JVM specification describes the method area as a logical part of the heap, it has a name called NON-HEAP (not a heap), which is intended to be associated with the Java heap partition.

The Java Virtual Machine specification has a very loose limit on the method area, and you can choose not to implement garbage collection. The garbage collection behavior is less common in this area, but the non-data entry method area is as permanent as the name of the permanent generation. The memory recovery target of this area is mainly for the recovery of constant pool and unloading of type, in general, the recovery of this area "score" is very difficult to be satisfactory, especially the type of unloading, the condition is very harsh, but this part of the area of the recovery is really necessary.

  

Instead, run a constant pool as part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is also a constant pool, for the compilation period generated by the various literal and symbolic references, which will be loaded in the class load into the method area of the run constant pool.

Java virtual machines have strict specifications for each part of the class file (which naturally includes Chang), and each byte is used to store which data must conform to the requirements of the specification to be recognized, loaded, and executed by the virtual machine, but for running a constant pool, the Java Virtual Machine specification does not require any details. Virtual machines implemented by different providers can implement this memory area according to their own needs.

Since the run-time-constant pool is part of the method area, it is naturally limited by the memory of the method area and throws a OutOfMemoryError exception when the constant pool is no longer able to apply to memory.

That's probably it, bye!

Java Virtual machine Performance monitoring and tuning combat

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.