Space complexity of algorithms (Java), algorithm space java

Source: Internet
Author: User

Space complexity of algorithms (Java), algorithm space java
0. Description

All the conclusions in this article have been referred to the standard literature and I have also verified them. This document does not describe the verification process. For example, the current mainstream Virtual Machine boolean Type Runtime is indeed 1 byte.
Some of the details that are irrelevant to the computing space are not described. For example, the specific information contained in the object header, the number of digits respectively, and what is Pointer compression.
For details, I will add them in the JVM analysis column in the future ~

I. Basic Knowledge 1. Data type and size
Basic Type
Type name Bytes occupied
Boolean 1
Byte 1
Char 2
Short 2
Int 4
Float 4
Long 8
Double 8
 Reference Type
Operating System bits Bytes occupied
32-bit 4
64-bit 8 (4 bytes after pointer compression)

* Note: The reference type (the reference type variable, not an object instance, is essentially a pointer, And the array type variable also belongs to the reference type variable ).

2. Memory Calculation Formula

Memory occupied by objects = Object Header overhead + instance data (if it is a reference type, it includes variable and instance overhead) + fill data.

A. Object Header overhead

Point to class references, garbage collection information, and synchronization information.
32-bit JVM object header 8 bytes, array object header 16 bytes.
64-bit JVM object header 16 bytes (pointer compressed 12 bytes), array object header 24 bytes (pointer compressed 16 bytes ).
Empty objects/empty arrays only have object headers.

B. instance data

The basic type has only the space occupied by the memory value.
The reference type contains the space occupied by the variables and values (the referenced variables are equivalent to pointers and are stored in a system storage unit. Value is the size of the heap instance ).

C. Fill in Data

In the hotspot JVM, the space occupied by objects is 8 bytes aligned. Because one storage unit in the memory is 8 bytes.
It means that the memory used by a Java object must be an integer multiple of 8 bytes. After calculation, it is found that the memory required by the object is not an integer multiple of 8 bytes, it will be filled with a multiple of 8 bytes.
For example, an object instance may require a memory of 30 bytes or 28 bytes and will be filled with 32 bytes.

D. inheritance relationship (the parent class Object Header overhead is not calculated)

Memory occupied by subclass objects = subclass object header overhead + subclass instance data + (parent instance data + fill data) + fill data.

Ii. Computing instances

Because the current mainstream systems are 64-bit systems, the following test class will calculate the memory according to the 64-bit environment. Pointer compression is not taken into account.
The test class demonstrates the most complex scenario. It only demonstrates the computing process and the name is not standardized. Do not imitate it in the actual project.

New B () memory usage = [(4 (a) + 2 (bs) + 2 (fill data) + 4 (ba) + 8 (cs array reference variable) + 4 (fill data)] + [24 (cs array object header) + 3 (number of referenced variables in the cs array, that is, cs [0], cs [1], cs [2]) * 8 (memory occupied by variables referenced in the cs array)] + [(16 (Bytes header information of the C instance) + 1 (c variable) + 7 (padding byte) * 3 (number of C instances)] = 144 bytes (an integer multiple of 8 bytes ).

Remarks

64-bit JVM supports pointer compression. -XX: + UseCompressedOops (Enabled)-XX:-UseCompressedOops (disabled ).
This function is added only after JDK1.6 and pointer compression is enabled by default. However, the formula remains unchanged, but the memory usage of some objects changes. You can calculate the memory usage after the compression pointer or under the 32-bit JVM.

The following two commands can be used to view VM parameter information such as pointer compression:
Jcmd // view process information (the PID is displayed in the numeric column. You can check the PID on the company server if you want to test the program during running ).
Jcmd [PID] VM. flags // view the JVM parameters set for the pid.

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.