Handles vs Direct Pointers: How Java Accesses Objects Efficiently
This article explains Java's object access mechanisms, detailing how references in the stack point to objects in the heap and type data in the method area, and compares the handle pool approach with direct pointer access, highlighting their respective performance and GC advantages.
Object access is ubiquitous in Java and involves three key memory regions: the Java stack , the Java heap , and the method area . The stack stores references, the heap holds the actual object instances, and the method area keeps type metadata such as class, superclass, interfaces, and method information.
The two mainstream ways to use a reference are handles and direct pointers .
1. Using Handles
When the handle approach is used, the heap allocates a handle pool. The reference stores the address of a handle, and each handle contains separate addresses for the object’s instance data and its type data, as illustrated below.
2. Using Direct Pointers
With direct pointers, the reference stores the object’s address directly. Consequently, the object layout in the heap must embed the necessary type‑information so that the JVM can resolve the class metadata without an extra indirection.
Both approaches have distinct advantages. Handles keep the reference stable; when the garbage collector moves an object, only the handle’s internal pointer changes, leaving the reference unchanged. Direct pointers are faster because they eliminate the extra level of indirection, reducing the time spent on pointer resolution—a significant benefit given the high frequency of object accesses in Java.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
