In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the problems of Java virtual machine, which can be used for reference. I hope you can learn a lot after reading this article. Let's take a look at it.
6.0.0.1 what are the runtime data areas? What does the Java virtual machine stack do? What does the local method stack do?
What are the runtime data regions?
It is part of the method area. In addition to the relevant version, field, method, interface and other description information in the Class file, another piece of information is the constant pool, which is used to store various literals and symbolic references generated during the editing period. This part of the content will be stored in the runtime pool of the method area after the class is loaded.
The Java language does not require that constants must be generated only during the editing period, that is, it is possible to put new constants into the pool. This feature is often used by developers is the intern () method of the String class.
An OutOfMemoryError exception is thrown when the constant pool can no longer apply for memory.
The method area is used to store class information, constants, static variables, compiled code and other data that have been loaded by the virtual machine.
In addition to the fact that the Java heap does not require contiguous memory and can choose to be fixed or scalable, you can also choose not to implement garbage collection. The goal of memory recovery in this area is mainly for constant pool recovery and type unloading.
When the method area cannot meet the memory allocation requirements, an OutOfMemoryErroy exception will be thrown
The heap is the largest block of memory managed by the Java virtual machine. The Java heap is a memory area shared by all threads, created when the virtual machine starts. The sole purpose of this memory area is to store object instances, where almost all object instances are allocated memory. All object instances and arrays are allocated on the heap
The Java heap is the main area managed by the garbage collector. The Java heap is subdivided into Cenozoic and old ages.
In any case, the purpose of partitioning is to better reclaim memory, or to allocate memory faster.
The Java heap can be in physically discontiguous memory space, as long as it is logically continuous. If instance allocation is not completed in the heap, and the heap cannot be extended, an OutOfMemoryError exception will be thrown
The roles of the local method stack and the virtual machine stack are very similar. The difference between them is that the virtual machine stack performs Java methods (that is, bytecode) services for the virtual machines, while the local method stacks serve the Native methods used by the virtual machines.
The local method stack area also throws StackOverflowError and OutOfMemoryErroy exceptions
Virtual machine stack describes the memory model of Java method execution: each method creates a stack frame to store local variables, Operand stack, dynamic link, method exit and so on. The process from the call to the completion of each method corresponds to a stack frame in the virtual machine stack from the stack to the unstack process.
Summary of technical blog
Stack memory is the virtual machine stack, or the part of the local variable table in the virtual machine stack.
The local variable table stores various basic data types known during editing (boolean, byte, char, short, int, float, long, double), object reference (refrence) type and returnAddress type (pointing to the address of a bytecode instruction)
Of these, 64-bit data of long and double types takes up two local variable spaces, while the rest of the data types occupy only one.
The Java virtual machine specification specifies two exception conditions for this area: if the stack depth of the thread request is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown. If the virtual machine cannot apply for enough memory when expanding, it will run out an OutOfMemoryError exception.
A program counter is a small piece of memory that can be thought of as a line number indicator executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed. Basic functions such as branching, loop, jump, exception handling, thread recovery and so on depend on this counter. If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the Native method is being executed, the counter is empty. This memory region is the only one that does not specify any OutOfMemotyError conditions in the Java virtual machine specification
The memory managed by the Java virtual machine includes several runtime data memory: method area, virtual machine stack, local method stack, heap, program counter, where the method area and heap are data areas shared by threads, and the others are thread-isolated data areas
1.1 Program counter
1.2 Java virtual machine stack
1.3 Local method stack
1.4 Java heap
1.5 method area
1.6 running constant pool
6.0.0.2 memory layout of objects? What are the ways to locate access to an object? What are the advantages of using pointer access and handle access?
The memory layout of the object?
A) store the object's own runtime data, such as hash code, GC banding age, lock status flag, lock held by thread, biased thread ID, biased timestamp
B) the other part refers to the type pointer, that is, the pointer of the object to its class metadata, through which the virtual machine determines that the object is an instance of that class
In HotSpot virtual machines, the layout of objects stored in memory can be divided into three areas: object headers, instance data, and alignment padding.
The object header consists of two parts:
What are the ways to locate access to an object?
The layout of Java heap objects must consider how to access information about type data, and what is stored in refreence is the address of the object directly.
A piece of memory will be divided in the Java heap to serve as a handle pool. What is stored in the reference is the handle address of the object, and the handle contains the specific address of the object instance data and the type data.
Use handle access
Use direct pointer access
What are the advantages of using pointer access and handle access?
Use handle access advantage: reference stores the handle address of a stable point, and only changes the instance data pointer in the handle when the object is moved (it is very common behavior when moving objects during garbage collection), but reference itself does not need to modify it.
The advantage of using direct pointer access: faster, saving a pointer positioning time overhead, because object access is very frequent in Java, so this kind of overhead can add up to a very considerable execution cost
Summary of technical blog
6.0.0.3 tell me about the process of creating objects? Where is the variable creation process located in the virtual machine?
Tell me about the process of creating objects? For example: Dog dog= new Dog ()
When the virtual machine executes the new instruction, it first looks for "Dog" in the constant pool to see if it can locate the symbolic reference of the Dog class; if it can, it indicates that the class has been loaded into the method area and continues to execute. If not, let Class Loader load the class first.
The virtual machine then begins to allocate memory for the object, and the amount of memory required by the object is determined after the class is loaded. At this point, all you have to do is allocate space on demand in the heap. There are two ways to allocate memory. First, the memory is absolutely regular, so as long as you place the pointer between the occupied memory and the free memory, and each time you allocate space, just move the pointer to the free memory space by a corresponding distance. When an object is reclaimed by GC, you need to migrate some object memory. Second, free memory and non-free memory are mixed together, so you need to use a list to record heap memory usage, and then allocate memory as needed.
In the case of multithreading, how do you ensure that when one thread allocates object memory but has not yet modified the memory management pointer, other threads allocate that block of memory and overwrite it? One way is to have each thread pre-allocate a small piece of memory in the heap (TLAB native thread allocation buffer), and each thread allocates memory only in its own memory. However, the object itself can be shared by threads according to its access properties.
After the memory is allocated, the virtual machine initializes the allocated memory space to zero (excluding object headers). Instance variables initialize the corresponding default values by variable type (numeric type is 0 and Boolan is false), so instance variables can be used without initial values. Then set the object header information, such as the hash value of the object, GC generation age and so on. Summary of technical blog
From a virtual machine point of view, a new object has been created at this point. However, from the point of view of running our program, the new object has just begun, and the constructor of the object has not been executed yet. Only after the constructor is executed and initialized according to the constructor, the object is completely created. The execution of the constructor also involves calling the parent constructor, which is automatically added if the parent constructor is not explicitly declared.
The new operator can return a reference to this object in the heap
Where is the variable creation process located in the virtual machine?
If the dog local variable, the dog variable is in the local variable table of the stack frame, the reference to this object is placed in the stack frame.
If dog is an instance variable, the dog variable is in the heap, and the reference to the object is placed in the heap.
If dog is a static variable and the dog variable is in the method area, the reference to the object is placed in the method area.
Variables are instance variables, local variables, or static variables that place references in different places:
6.0.0.4 in which data areas may OutOfMemoryError exceptions occur? What are the scenarios and reasons for the emergence of OOM in this data area?
In which data areas are OutOfMemoryError exceptions likely to occur?
Java heap overflow
Virtual machine stack and local method stack overflow
Method zone and runtime constant pool overflow
What are the scenarios and reasons for the emergence of OOM in this data area?
String.intern () is a Native method that returns a String object representing the string in the pool if there is already a string in the constant pool; otherwise, the string contained in the String object is added to the constant pool and a reference to the String object is returned
Because constant pools are allocated in permanent generations, you can limit the size of the method area through-XX:PermSize and-XX:MaxPermSize, thus indirectly limiting the capacity of constant pools. Summary of technical blog
Intern (): the JDK1.6 intern method copies the first encountered string instance to the permanent generation, and returns a reference to this string instance in the permanent generation, while the string instance created by StringBuilder is on the Java heap, so it must not be a reference. The implementation of the JDK1.7 intern () method no longer copies the instance, but records the instance reference that first occurs in the constant pool, so the reference returned by intern () is the same as the string instance created by StringBuilder.
For HotSpot, although the-Xoss parameter (setting the local method stack size) exists, it is actually invalid, and the stack capacity is only set by the-Xss parameter. With regard to the virtual machine stack and local method stack, two exceptions are described in the Java virtual machine specification:
If the stack depth requested by the thread is greater than the maximum allowed by the virtual machine, a StackOverflowError will be thrown
If the virtual machine cannot apply for enough memory space when expanding the stack, an OutOfMemoryError exception is thrown
Under single thread, no matter because the stack frame is too large or the virtual machine stack capacity is too small, when the memory cannot be allocated, the virtual machine throws a StackOverflowError exception.
If the memory overflow is caused by multithreading, it has nothing to do with whether the stack space is large enough or not. at this time, the larger the memory allocated by each thread's stack, the more likely it is to produce a memory overflow exception. The solution is that when the number of threads cannot be reduced or the 64 virtual machines cannot be replaced, more threads can only be obtained by reducing the maximum heap and stack capacity.
Java heap is used to store object instances. As long as you constantly create objects and ensure that there is a reachable path between GCRoots and objects to prevent garbage collection from clearing these objects, a memory overflow exception will occur when the number reaches the maximum heap capacity limit.
If it is a memory leak, you can further check the reference chain from the leaked object to GC Roots through the tool. Then you can find out how the leaked objects are associated with the GC Roots and cause the garbage collector to fail to collect them automatically. By mastering the type information of the leaked object and the information of the GC Roots reference chain, the location of the leaked code can be located more accurately.
If there is no leak, in other words, all objects in memory really must still be alive, then you should check the heap parameters of the virtual machine (- Xmx and-Xms) to see if they can be enlarged compared with the physical memory of the machine, check from the code whether there are cases where the life cycle of some objects is too long and hold the state for too long, and try to reduce the memory consumption during the run-time of the program.
Java heap overflow
Virtual machine stack and local method stack overflow
Method zone and runtime constant pool overflow
6.0.0.6 what is the difference between heap and stack in Java? Write programs for heap memory overflow and stack memory overflow respectively?
What is the difference between heap and stack in Java?
Stack memory: mainly used to store basic data types and local variables; when a variable is defined in a code block, memory space is allocated in the stack for that variable, and this space is automatically freed when it exceeds the scope of the variable.
Heap memory: used to store objects created at run time, such as objects and arrays created by the new keyword; it needs to be managed by the automatic garbage collector of the Java virtual machine.
Write programs for heap memory overflow and stack memory overflow respectively?
Stack memory overflow
Public void A () {A ();}
Heap memory overflow
Public void testd () {List list = new ArrayList (); int I = 0 position while (true) {list.add (new String (I + "")); iConjects;} 6.0.0.7 if the object's reference is set to null, will the garbage collector immediately release the memory occupied by the object?
If the reference to the object is set to null, will the garbage collector release the memory occupied by the object immediately?
No, this object will be recyclable in the next garbage collection cycle.
That is, when an object's reference becomes null, it is not immediately reclaimed by the garbage collector, but the memory it occupies is freed the next time the garbage is collected.
What are the methods of garbage collection in 6.0.0.8 java?
What are the methods of garbage collection in java
Today's virtual machine garbage collection is mostly used in this way, which divides the heap into the new generation and the old age according to the life cycle of the object. In the new generation, due to the short lifetime of objects, a large number of objects will die each time they are recycled, so the replication algorithm is adopted. In the old days, objects had a high survival rate, and there was no extra space to allocate guarantees, so mark-organize or mark-clear could be used.
The main purpose of this algorithm is to solve the problem of mark-removal, resulting in a large number of memory fragments; when the object survival rate is high, it also solves the efficiency problem of the replication algorithm. The difference is that when the object is cleared, the recyclable object is now moved to one end, and then the object outside the drop boundary is cleared, so that there is no memory fragmentation.
In order to solve the problem of efficiency, the replication algorithm divides the available memory into two equal parts according to capacity, and then uses only one of them at a time. When one piece of memory is used up, the surviving objects are copied to the second block of memory. Then the first block of memory is clear at one time, and then the objects on the second block are copied to the first block. But in this way, the cost of memory is too high, basically wasting ordinary memory every time.
So the algorithm is improved, the memory area is no longer divided according to 1:1, but the memory is divided into three parts at 8:1:1, the larger part of the memory is transferred to the Eden area, and the remaining two smaller memory areas are called Survior areas. Every time, the Eden area is preferred. If the Eden area is full, the objects are copied to the second memory area, and then the Eden area is cleared. If there are too many objects alive at this time that there is not enough Survivor, these objects will be copied to the old age through the allocation guarantee mechanism. (java reactor is divided into Cenozoic era and old age)
This is the most basic garbage collection algorithm, according to the name can know, its idea is to mark which objects to be recycled, and then unified recycling. This method is simple, but there are two main problems: 1. The efficiency is not high, the efficiency of marking and removal is very low; 2. A large number of discontiguous memory fragments will be generated, causing the program to trigger a GC action in advance because there is not enough continuous memory when the program allocates larger objects.
Mark-clear:
Replication algorithm:
Tag-organize the summary of the technical blog
Generational collection
6.0.1.1 how to judge whether an object is alive or not? Which is better, reference counting method or reachability algorithm? How to understand that an object may not be recycled?
The so-called reference counting method is to set a reference counter for each object. whenever there is a place to reference the object, the counter is increased by one, and when the reference expires, the counter is reduced by one. When an object's reference counter is 00:00, the object is not referenced, that is, a "dead object" and will be garbage collected.
A defect of reference counting method is that it can not solve the problem of circular reference, that is to say, when object A refers to object B, and object B refers to object A, then the reference counter of AMagi B object is not zero, which makes it impossible to complete garbage collection, so the mainstream virtual machines do not adopt this algorithm.
Citation counting method
two。 Reachability algorithm (reference chain method)
Objects referenced in the virtual machine stack
The object referenced by the static property of the method area class
Objects referenced by the method area constant pool
Objects referenced by the local method stack JNI
The idea of the algorithm is to search down from an object called GC Roots, and if an object is not linked to GC Roots with any references, the object is not available.
There are several kinds of objects that can be used as GC Roots in java:
How to understand that an object may not be recycled? Summary of technical blog
Although these algorithms can determine whether an object can be recycled, an object ratio may not be recycled when the above conditions are met. When an object is unreachable to GC Root, the object is not immediately reclaimed, but is in a reprieve phase, and it needs to be marked twice to be truly recycled.
If the object does not have a reference chain with GCRoot in the reachability analysis, it is marked for the first time and filtered if it is necessary to execute the finalize () method. When the object does not override the finalize () method or has been called by the virtual machine, it is considered unnecessary.
If it is necessary for the object to execute the finalize () method, the object will be placed in a pair queue called F-Queue, and the virtual machine will trigger a Finalize () thread to execute, which is a low priority thread, and the virtual machine will not promise to wait for it to finish, because if the execution of finalize () is slow or deadlock occurs, it will cause the F-Queue queue to wait. Caused the crash of the memory recovery system. GC marks an object in F-Queue for the second time, at which point the object is removed from the "about to recycle" collection, waiting for recycling.
6.0.1.2 the difference between Class.forName () and ClassLoader.loadClass ()?
The difference between Class.forName () and ClassLoader.loadClass ()?
The question is reflection, but at the bottom it involves the knowledge of virtual machine class loading.
Class.forName () performs the connection and initialization action during class loading by default. Once the initialization action is performed, the static variable will be initialized to the value set by the programmer. If there is a static code block, the static code block will also be executed.
By default, ClassLoader.loadClass () only executes the loading action during class loading, and none of the subsequent actions will be performed.
Thank you for reading this article carefully. I hope the article "what are the problems with Java virtual machines" shared by the editor will be helpful to you? at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.