01JVM_ memory structure

1. What is JVM

1. Definition of JVM

The running environment of the Java program, the running environment of the java binary bytecode

2. Benefits of JVM

① Write once, run everywhere

② Automatic memory management, garbage collection function

③Array subscript out-of-bounds check

④ Polymorphism

3. Comparison of jvm , jre, jdk

 3. Common JVMs

The main learning is the HotSpot virtual machine

4.jvm learning route

①ClassLoader: After the java code is compiled into binary, it will pass through the class loader, so that it can be loaded into the JVM to run

②Method Area: The class is placed in the method area

③Heap: The instantiated object of the class is placed in the heap area

④ When a class calls a method, the virtual machine stack, program counter, and local method stack are used

⑤The execution of each line of code of the method is executed line by line in the interpreter of the execution engine, and the hot code of the frequently called method is executed in the JIT instant compiler. GC garbage collection recycles unused objects on the heap

⑥ It is the local method interface that needs to deal with the operating system.

2. Memory structure

1. Program Counter

① Definition

The program counter (register) records the execution address of the next jvm instruction

②Features

Thread private, there will be no memory overflow.

2. Virtual machine stack

definition:

①The memory space required by each thread to run is called the virtual machine stack.

②Each stack is composed of a stack frame Frame, which corresponds to the memory occupied each time a method is called (parameters, local variables, return address)

③Each thread can only have one active stack frame, corresponding to the currently executing method

problem analysis

① Does garbage collection involve stack memory?

Won't. Stack memory consists of stack frames, corresponding to the memory occupied by each method call. The stack is automatically popped at the end of each method call.

②Is the larger the stack memory allocation, the better?

no. The default stack memory is 1M, and the physical memory is fixed, so the larger the stack memory, the more recursive calls can be supported. But there will be fewer threads.

③Are the local variables in the method thread-safe?

Thread-safe: local variables within methods and no return value.

Thread unsafe: Local variables within methods have return values. parameters within the method.

Stack overflow java.lang.stackOverflowError

①Too many stack frames (method recursive call)

②The stack frame is too large

③ Operation of third-party class libraries

Modify stack memory size -Xss

 Stack Overflow Thread Run Diagnostics

Case 1: How to troubleshoot excessive CPU usage

①Use the top command to view the process with the highest CPU usage

ps H -eo pid,tid,%cpu|grep process id to further locate which thread caused the high CPU usage

The jstack process id can find the problematic thread according to the thread id

3. Native method stack

 There are methods with the native keyword in the local method stack , and the function is that java calls the local C or C++ method to interact with the bottom layer of the system.

4. Heap

Heap _

Through the new keyword, creating objects will use heap memory

characteristics of the heap

①Thread sharing, objects in the heap need to consider thread safety issues

② There is a garbage collection mechanism

heap overflow

A large number of objects occupy the heap memory space java.lang.OutOfMemoryError: java heap space causes heap memory space to overflow

Use -Xmx memory size   to modify heap memory

Troubleshoot the cause of heap memory overflow

①jps tool

Check which java processes exist in the current system

②jmap tool

View heap memory usage jmap -heap process id

③jconsole tool

Image interface, multi-function detection, continuous monitoring

5. Method area

definition

①Shared by threads in the method area (heap is also)

②The method area is created when the JVM starts and the memory space is not continuous, which can be expanded

③The method area is similar to the storage area of ​​compiled code. The method area is to store class information (member variables, method data, member methods and constructor codes)

Method area composition

①In jdk1.6:

The method area is conceptual, and the method area is implemented with the PermGen permanent generation. Storage class information, storage class loader, runtime constant pool

②In jdk1.8:

The method area is conceptual, and the method area is implemented with Metaspace cloud space (using system memory, not managed by the JVM, but managed by the operating system). Storage class information, storage class loader, runtime constant pool

Method area memory overflow class loading too much

①Before 1.8: Permanent generation memory overflow java.lang.OutOfMemoryError: PermGen

Use -XX:Max PermSize=8m to specify the permanent generation memory size

②After 1.8: Metaspace memory overflow java.lang.OutOfMemoryError: Metaspace

Use -XX:Max MetaspaceSize =8m to specify the metaspace memory size

overflow scene

Spring - generates a large number of classes

My shoe

runtime constant pool

 

①Constant pool

The constant pool is a constant table. The virtual machine instruction finds the class name, method name, parameter type, literal value and other information to be executed according to this constant table.

②Runtime constant pool

The constant pool is a *.class file. When the class is loaded, the constant pool information will be put into the runtime constant pool, and the symbolic address inside will be changed to a real address.

5.1StringTable interview questions

characteristic

① The string in the constant pool is just a symbol, and it becomes an object when it is used for the first time

② Use the string mechanism to avoid creating duplicate string objects

③The splicing of string variables is StringBuilder

④ The splicing of string constants is optimized by the compiler

⑤Use the intern method to put the string object into the string pool

The relationship between constant pool and string pool

 

① The information of the constant pool at runtime will be loaded into the running constant pool. At this time, ab and ab are all objects of the constant pool, and have not yet become java string objects. When the response code is executed and then created

②ldc #2 will turn a symbol into "a" string object and put it into StringTable

③ldc #3 will turn the b symbol into a "b" string object and put it into StringTable

④ldc #4 will turn the ab symbol into an "ab" string object and put it into StringTable

⑤StringTable[a,b,ab] hashtable structure, cannot be expanded, not repeated

String variable concatenation

Q: s3==s4? False.

The ab of s3 is in the string constant pool, and s4 is a variable splicing new String ("ab") to create an object in the heap. The address is not the same is false

compiler optimization

Q: s3==s5? True

The ab of s3 is in the string constant pool. When s5 performs string constant splicing , it will check whether it exists from the constant pool, and if s5 exists, it will point to ab in the constant pool

intern_jdk1.8

s.intern(); try to put this string object s into the string pool, if the string pool has it, return ab address, if not, put it into the return object s address

analyze:

 

 

intern_jdk1.6

s.intern(); try to put this string object s into the string pool, if the string pool has it, return the ab address, if not, put it into the return ab new address

In the above analysis, when there is no ab in the constant pool, s2=s.intern() puts ab, and the return is not the address of s

interview questions

5.2StringTable position 

 

①jvm memory structure 1.6

The memory garbage collection efficiency is low when the permanent generation only has full GC, and StringTable is part of the constant pool

②jvm memory structure 1.8

Garbage collection is efficient and is part of the heap

5.3 StringTable can be garbage collected

Garbage collection occurs when memory is tight

5.4 StringTable Performance Tuning

①Because StringTable is implemented by HashTable, increase the number of HashTable buckets and reduce the time for strings to be put into the string pool

-XX:StringTableSize=xxxx

//Minimum is 1009

② Use the intern method to reduce repeated entry into the pool, and ensure that only one copy of the same address is stored in StringTable.

6. Direct Memory Direct Memory

definition

① Commonly used in NIO operations for data buffers

② Allocation and recovery costs are high, but read and write performance is high

③ Not subject to JVM memory recovery management

basic use

Direct Memory Direct memory is an area that both the operating system and java code can access. When the disk file is read, it reaches the direct memory, and the java code can also access the direct memory.

out of memory

① Direct memory can also cause memory overflow problems

②OutOfMemoryError:Direct buffer memory

release principle

The recovery of direct memory is not released through garbage collection of the JVM, but manually released through unsafe.freeMemory

Principles of direct memory allocation and recovery

① Use unsafe objects to complete the allocation and recovery of direct memory, and the recovery needs to manually call the freeMemory method

② Inside the ByteBuffer implementation class, the Cleaner virtual reference is used to monitor the ByteBuffer object. Once the ByteBuffer object is garbage collected, the ReferenceHandler daemon thread will call freeMemory through the Cleaner 's clean method to release the direct memory.

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/132605636