Access to the "2" Jvm-java object

Source: Internet
Author: User

Access to objects in Java

Java is an object-oriented language, so there are so many objects in the Java virtual machine that object access is ubiquitous. Instant is the simplest access to the Java stack, Java heap, the method area of the three very important memory area of the association between.

Like what:

Object obj = new Object ();

Where the semantics of "Object obj" appear as a reference type data, it is stored in the local variable table of the Java stack. New Object () will generate an entity object, stored in the Java heap, containing the structured memory of all instance data values of type object (data of individual fields in the objects), and the length of the memory is not fixed, depending on the type and the layout of the object memory implemented by the virtual machine. Also, in the Java heap, you must include address information that can find data for this object type, such as Object types, parent classes, implementing interfaces, methods, and so on, which are stored in the method area.

Because the reference type only specifies a reference to the object in the Java Virtual Machine specification, and does not define the way in which the reference is to be located, and the specific location of the objects in the Java heap, the object access methods implemented by different virtual machines will vary. There are two ways to access the mainstream: handles and direct pointers.

    • Handle

The Java heap will be divided into a block of memory as a handle pool, reference is stored in the object's handle address, and the handle contains the object instance data and the type data of the respective address information.

The biggest benefit of using handle access is that reference stores a stable handle address that changes only the instance data pointer in the handle when the object is moved, and reference itself does not need to be modified.

    • Direct pointer

Compared to how the handle is accessed, the Java heap does not separate the memory, the object address is stored directly in the reference, and the object contains the address information of the object type data.

The biggest benefit of using direct pointers is faster, saving time spent on pointer positioning, and because of the frequent access to Java objects, this kind of overhead is a significant execution cost. This is how the Sun hotspot virtual machine uses this access.

An entity class, named Stu:

[HTML]View PlainCopy  print?
    1. public class Stu extends object{
    2. private String name;
    3. private int age;
    4. Public Stu (String name,string age) {
    5. this.name = name;
    6. this.age = Age ;
    7. }
    8. Public String GetName () {
    9. return this.name;
    10. }
    11. ...
    12. }

To create a Stu object:

Stu Kevin = new Stu ("Kevin", 15);

This is explained as follows:

Kevin is stored in a local variable table as a variable of type reference, and in the hot spot virtual machine, the direct address of the (kevin=) specific object is stored; new Stu ("Kevin", 15) is an instantiation of an object, All field information for Stu entity classes in the Java heap, such as name= "Kevin", age=15. At this point, the Java heap also stores the Stu object's type data address information, through this address in the method area can find the type of object, the parent class, the implementation of the interface, methods and other information.

Access to the "2" Jvm-java object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.