The size of the memory that the underlying type encapsulates an object in Java (RPM)

Source: Internet
Author: User

This is a program, Java does not have the implementation of the existing sizeof, the main reason is that the basic data type in Java is a fixed size, so it seems unnecessary to use the keyword sizeof. The idea of implementation is this: there are some simple functions in the Java.lang.Runtime class that can involve memory management: Every Java application have a single instance of classThat allows the application to interface with the environment in which the application is Runtime running. The current runtime can is obtained from the getRuntime method.

The

example is well written to illustrate the size of the memory that the basic type encapsulates in Java.   
  1. A Simple Object object consumes 8 bytes of memory space, because each instance must contain at least some of the most basic operations, such as: Wait ()/notify (), Equals (),   hashcode (), and so on   
  2. Using an Integer object consumes 16 bytes, while int occupies 4 bytes, which says that the memory consumption is 4 times times larger after the encapsulation   
  3. Then long appears to use more space than an integer object, resulting in a long space of 16 bytes.   
  That's exactly what the JVM's rules for allocating memory for a basic type encapsulated object are as follows:    
  Object occupies memory (8 bytes) + Maximum base type (long) of memory (8 bytes)   =   16 bytes.   
  JVM enforces the use of 8 bytes as a boundary.    
  So the size of the memory for all the basic types of encapsulated objects is 16 bytes. But there are still differences, such as: I The Nteger object consumes 16 bytes of memory, but only takes advantage of the memory (8 bytes) of the object +int the memory (4 bytes)   =   12 bytes. and 4 bytes are not used at all. Oh, careful analysis of the night, there are a lot of harvest .   

http://blog.csdn.net/dkarthas/article/details/1570469

Related Article

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.