In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the concept and function of memory region and object in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the concept and function of memory region and object in Java".
1. The Java virtual machine contains the following runtime data areas: program counter, Java virtual machine stack, local method stack, Java heap, method area.
2. Program counter
1) in the conceptual model of the virtual machine, the bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed. Branch, loop, jump, exception handling, thread recovery and so on all depend on the counter.
2) in order to restore to the correct execution position after thread switching, each thread needs an independent program counter, which does not affect each other and is stored independently, so it is thread independent.
3) if a Java method is being executed, the counter records the address of the virtual machine bytecode instruction being executed; if the Native method is being executed, the value of the counter is empty
4) this memory region is the only one that does not specify any OutOfMemoryError conditions in the Java virtual machine specification.
3. Java virtual machine stack
1) Thread is private and its life cycle is the same as thread
2) it describes the memory model of Java method execution: when each method is executed, a stack frame is created to store local variables, Operand stack, dynamic link, method exit and other information. From the call to the execution of each method, it corresponds to the process of a stack frame going into and out of the stack in the virtual machine stack.
3) two kinds of anomalies: StackOverflowError and OutOfMemoryError.
4. Local method stack
1) similar to the virtual machine stack, except that the virtual machine stack performs Java method services for the virtual machine, while the local method stack serves the Native method used by the virtual machine.
2) two kinds of anomalies: StackOverflowError and OutOfMemoryError.
5. Java heap
1) all threads share and store object instances and arrays
2) the Java heap is the main area managed by the garbage collector
3) the Java heap can be in physically discontinuous memory space, as long as it is logically continuous.
4) if there is no memory to complete the instance allocation and the heap cannot be expanded, an OutOfMemoryError exception will be thrown.
6. Method area
1) all threads share and store data such as class information, constants, static variables, compiled code, etc., that are loaded by the virtual machine.
2) JDK1.8 still retains the concept of method area, but it is implemented differently, the permanent generation is cancelled, and the method is stored in metaspace (Metaspace), which is still not connected to the heap, but shares physical memory with the heap, which can be logically considered in the heap.
3) if the memory allocation requirement cannot be met, an OutOfMemoryError exception will be thrown.
7. The process of creating an object using new
1) Class loading check. First, check whether the parameter of this instruction can locate a symbol reference in the constant pool, and check whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, the class loading process must be performed.
2) allocate memory. There are two ways of allocation: pointer collisions and free lists. Pointer collisions require Java heap memory to be absolutely regular, and free lists allow Java heap irregularities. That method depends on the garbage collector used.
3) consider the concurrency problem. There are two options: one is that the CAS+ fails to retry to ensure atomicity, and the other is the local thread allocation buffer (TLAB), which allocates a small piece of memory to each thread in advance and requires synchronous locking after it is allocated.
4) initialize zero. Make sure that the instance field of the object can be used directly without an initial value in the Java code.
5) set object information. For example, the instance of which class the object is, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object and so on. This information is stored in the object header.
6) execution method.
8. Memory layout of objects
1) 3 areas: object header, instance data, alignment fill
2) object header: contains two parts of information, one is used to store its own run-time data, such as hash code, GC generational age, lock status flag, lock held by thread, ID, timestamp, etc., and the other part is a type pointer, a pointer to its class metadata.
3) instance data: it is the valid information really stored by an object.
4) alignment padding: it is not inevitable, because HotpotVM's automatic memory management system requires that the starting address of the object must be an integer multiple of 8 bytes, so the part that is not aligned needs to be filled.
9. Object access location: the mainstream access methods are handle and direct pointer, while HotSpt uses the second way to access objects.
Thank you for your reading, the above is the content of "the concept and function of memory region and object in Java". After the study of this article, I believe you have a deeper understanding of the concept and function of memory region and object in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.