国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java JavaBase Detailed explanation of JVM object creation and access positioning process

Detailed explanation of JVM object creation and access positioning process

Dec 08, 2020 pm 05:50 PM
java jvm Create object

java Basic TutorialThe column introduces the process of JVM creating objects and accessing positioning

Detailed explanation of JVM object creation and access positioning process

Related free learning recommendations: java basics Tutorial

1. Object creation

  • When the virtual machine receives the new instruction, it checks whether this instruction can locate a class symbol in the constant pool. reference, and checks whether the class represented by this symbolic reference has been loaded, resolved, and initialized. If there are none, perform the class loading process first.
  • After the class loading is passed, the virtual machine allocates memory for the new object (dividing a memory of a certain size from the Java heap). The memory size can be completely determined after the class loading is completed.
  • Two allocation methods:
    • (1): Pointer collision: Assume that the memory in the Java heap is absolutely regular, that is, the used memory is on one side, the free memory is on the other side, and the middle A pointer is placed as an indicator, and memory allocation is achieved by moving the pointer.
    • (2): Free list: If the memory in the Java heap is not regular, that is, used memory and free memory are interleaved, the virtual machine must maintain a list to record which memory blocks are available. , allocates memory by finding space from the list to allocate to object instances.
  • Whether the Java heap is regular or not is determined by whether the garbage collector used has a compression and finishing function.
  • Creating objects in a virtual machine is not a thread-safe behavior. It may occur when object A is allocated memory and the pointer has not had time to be modified, and object B uses the original pointer to allocate memory. There are two solutions:
  • (1): Synchronize the action of allocating memory space. In fact, the virtual machine uses CAS with failure retry to ensure the atomicity of the update operation;
  • (2): The memory allocation action is divided into different spaces according to threads, that is, each thread pre-allocates a small piece of memory in the Java heap, called the local thread allocation buffer (Thread Loal Allocation Buffer, TLAB) .
  • After the memory allocation is completed, the allocated memory space needs to be initialized to zero values ??to ensure that the instance fields of the object can be used directly in the Java code without assigning initial values, and the program can access these fields. The zero value corresponding to the data type.
  • Set the object, store which class the object is an instance of, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object, etc. in the object header.

2. Memory layout of objects: The layout of objects stored in memory can be divided into three parts: object header (Header), instance data (Instance Data), and alignment padding (Padding). The object header includes two parts of information:

(1): Stores the runtime data of the object itself, such as hash code, GC generation age, lock status flag, lock held by the thread, and bias Thread ID, bias timestamp, etc. The length of this part of data is 32bit and 64bit in 32-bit and 64-bit virtual machines respectively. It is officially called Mark Word (a non-fixed data structure that multiplexes its own storage according to the state of the object). space).

#(2): Type pointer, that is, a pointer to the class metadata of the object. The virtual machine uses this pointer to determine which class the object is an instance of.

  • Instance data: The effective information actually stored by the object, that is, the contents of various types of fields defined in the program code. Whether it is inherited from the parent class or defined by the subclass itself, it needs to be recorded.
  • Alignment padding: It does not necessarily exist and serves as a placeholder. Since HotSpot VM requires that the size of the object must be an integer multiple of 8 bytes, and the object header part is exactly an integer multiple of 8 bytes, Therefore, when the instance data is not aligned, it is completed by alignment padding.

3. Object access positioning: Java operates specific objects on the heap through reference data on the stack (object references in local variable tables). Reference only specifies references to objects, not Define how to locate and access the location of objects in the heap. The object access method is implemented by Swift.

(1): Handle access: The Java heap will divide a piece of memory as a handle pool. The reference stores the handle address of the object. The handle contains the address information of the object instance data and type data.

Advantages: The reference stores a stable handle address. When the object moves, only the instance data pointer in the handle changes, not the reference.

(2): Direct pointer: What is stored in the reference is directly the object address, and the address of the accessed object type data (stored in the method area) is placed in the Java heap.

Advantages: Faster, saving the time overhead of pointer positioning. HotSpot uses direct pointer access.

The above is the detailed content of Detailed explanation of JVM object creation and access positioning process. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1501
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

How does garbage collection work in Java? How does garbage collection work in Java? Aug 02, 2025 pm 01:55 PM

Java's garbage collection (GC) is a mechanism that automatically manages memory, which reduces the risk of memory leakage by reclaiming unreachable objects. 1.GC judges the accessibility of the object from the root object (such as stack variables, active threads, static fields, etc.), and unreachable objects are marked as garbage. 2. Based on the mark-clearing algorithm, mark all reachable objects and clear unmarked objects. 3. Adopt a generational collection strategy: the new generation (Eden, S0, S1) frequently executes MinorGC; the elderly performs less but takes longer to perform MajorGC; Metaspace stores class metadata. 4. JVM provides a variety of GC devices: SerialGC is suitable for small applications; ParallelGC improves throughput; CMS reduces

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

go by example defer statement explained go by example defer statement explained Aug 02, 2025 am 06:26 AM

defer is used to perform specified operations before the function returns, such as cleaning resources; parameters are evaluated immediately when defer, and the functions are executed in the order of last-in-first-out (LIFO); 1. Multiple defers are executed in reverse order of declarations; 2. Commonly used for secure cleaning such as file closing; 3. The named return value can be modified; 4. It will be executed even if panic occurs, suitable for recovery; 5. Avoid abuse of defer in loops to prevent resource leakage; correct use can improve code security and readability.

Comparing Java Build Tools: Maven vs. Gradle Comparing Java Build Tools: Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

Java Concurrency Utilities: ExecutorService and Fork/Join Java Concurrency Utilities: ExecutorService and Fork/Join Aug 03, 2025 am 01:54 AM

ExecutorService is suitable for asynchronous execution of independent tasks, such as I/O operations or timing tasks, using thread pool to manage concurrency, submit Runnable or Callable tasks through submit, and obtain results with Future. Pay attention to the risk of unbounded queues and explicitly close the thread pool; 2. The Fork/Join framework is designed for split-and-governance CPU-intensive tasks, based on partitioning and controversy methods and work-stealing algorithms, and realizes recursive splitting of tasks through RecursiveTask or RecursiveAction, which is scheduled and executed by ForkJoinPool. It is suitable for large array summation and sorting scenarios. The split threshold should be set reasonably to avoid overhead; 3. Selection basis: Independent

See all articles