Calculate how many bytes a Java object occupies _java

Source: Internet
Author: User
Tags array length memory usage

The example in this article describes how to calculate (or estimate) the amount of memory a Java object occupies. Share to everyone for your reference. The specific analysis is as follows:

In general, the premise of the heap memory usage we are talking about is the "general situation" background. Does not include the following two scenarios:

In some cases, the JVM simply does not put object in the heap. For example: In principle, a small thread-local object exists in the stack, not in the heap.
The size of memory consumed by object depends on the current state of object. For example, whether the synchronization lock for object is in effect or if object is being reclaimed.
Let's take a look at what a single object looks like in the heap.

In the heap, each object is composed of four domains (A, B, C, and D), and we explain each one individually:

A: Object header, takes up a small number of bytes, expresses the current state of the object information
B: The space occupied by the basic type domain (the native domain means int, boolean, short, etc.)
C: The space occupied by the Reference type field (reference Type field refers to other objects, each reference occupies 4 bytes)
D: The space occupied by the filler (the following indicates what is the filler)

Here we explain a, B, C and D

A: Object Headers
in memory, the total space occupied by each object not only contains the space required for variables declared within the object, but also includes additional information such as object headers and fillers. The purpose of the object header is to record the instance name, ID, and instance state of an object (for example, whether the current instance is "reachable", or the current lock state, and so on).
In the current JVM version (Hotspot), the number of bytes occupied by the object header is as follows:

A common object that takes up 8 bytes
Array, occupies a bytes, contains 8 bytes + 4 bytes of ordinary objects (array length)

B: Basic Types

Boolean, byte occupies 1 Byte,char, short occupies 2 bytes,int, float occupies 4 bytes,long, double occupies 8 bytes

C: Reference type
Each reference type occupies 4 bytes

D: Filling Material
in hotspot, the total space occupied by each object is calculated in multiples of 8, and the object takes up the total space (object Head + declaration variable) at a multiple of less than 8, automatically padded. And, these filled spaces, we can call it "filler." Let's look at the specific example:

An empty object (without declaring any variables) occupies 8 bytes--> object Header occupies 8 bytes
A class that declares only a Boolean type variable, occupies the Bytes--> object Header (8 bytes) + Boolean (1 bytes) + filler (7 bytes)
Class that declares 8 Boolean variables, occupies the Bytes--> object Header (8 bytes) + Boolean (1 bytes) * 8

The above example helps us to deepen our understanding of Java programming.

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.