A Java object in the end how much?

Read this article takes about 2.8 minutes.

Source: http: //u6.gg/swLPg

Writing Java code, in most cases, we paid little attention to how much a Java object (occupy much memory), and more of a focus on business logic.

But everyone knows, inadvertently between us, a lot of wasted memory is invisible.

A Java object in the end how much?

Want to calculate a precise amount of memory Java objects, we must first understand the structure of a Java object representation.

Java object structure

Heap of a Java object representation can be divided into three parts:

  • Object Header

  • Class Pointer

  • Fields

Each ordinary Java objects in the heap (heap) has a header information (object header), header information is essential to record the state of the object.

32-bit and 64-bit space is different in the 32-bit:

hash(25)+age(4)+lock(3)=32bit

64 in which:

unused(25+1)+hash(31)+age(4)+lock(3)=64bit

We know that in Java, everything is an object; each class has a parent class, a pointer ClassPointer is the current object's parent class.

In the 32-bit system, this pointer 4byte;

In the 64-bit system, if enabled pointer compression (-XX: + UseCompressedOops) or less than the maximum JVM stack 32G, this pointer is 4byte, otherwise 8byte.

About Fields (Fields), here it refers to an instance of the class field; that does not include a static field, because the field is shared memory, there will be only one.

Below is an example of the system 32, in the end java.lang.Integer calculate how much memory occupied:

Pointer ObjectHeader are fixed and, 4 + 4 = 8byte.

Look at the field, only this one, a numerical value:

/** * The value of the <code>Integer</code>. * * @serial */private final int value;

Int occupies a 4byte in java, so that the size of the Integer is 4 + 4 + 4 = 12byte.

This result is right?

wrong!

Another point did not say: in java, occupied by the object heap size is 8 aligned above 12byte not aligned, you need to fill the seats 4byte. The result is 16byte!

In addition, in Java there is a special object, an array!

Yes, this object is a bit special, it is one more attribute than other objects: length (length).

So we compute the array length of time, allow an additional length of a field, which is the size of an int.

For example: int [] arr = new int [10];

Arr occupied heap size is:

4 (object header) +4 (pointer) +4 (length) + 4 * 10 (10 size int.) = 52byte aligned eight due to the need, the final size of 56byte.

Saving memory guidelines

After understanding the memory usage of objects, we can simply count an account.

Java.lang.Integer occupies a 16byte, and an int occupied 4byte, 4: 1 ratio.

That kind of type integer is four times the basic type of memory!

This leads us to the principle of first save memory:

(1) make use of the basic type instead of the type of packaging.

Build a database table when field types require careful scrutiny, the same type of JavaBean property field are also required careful consideration.

Do not mean to use short, byte, boolean, if short data type can put down, try not to use longer types.

Than a long int just over a 4byte, but you want to, if there is 100W a long memory, it is about 4MB wasted space, do not underestimate this little bit of wasted space, because just a ran online application JVM , the object can reach tens of millions!

Memory is freed up.

(2) the appropriate field type, the premise to meet the capacity, as much as possible with a small field.

Do you know an ArrayList collection, which if put 10 numbers, how much memory it occupied?

Let's calculate:

ArrayList in two fields:

/** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. */private transient Object[] elementData;/** * The size of the ArrayList (the number of elements it contains). * * @serial */private int size;

Object Header accounting 4byte, Pointer accounting 4byte, a field int (size) representing 4byte, elementData 12 representing the array itself (4 + 4 + 4), the array 10 Integer object representing 10 × 16.

Therefore, the entire collection space is 4 + 4 + 4 + 12 + 160 = 184byte.

If we use the int [] set it in place, 12 + 4 × 10 = 52byte, after its 56byte.

Collection with an array of proportion is 184: 56, more than 3: 1 up!

(3) If possible, use the array, less set.

The array can use basic types, but only put the collection type of packaging!

If you really need to use the collection, a collection of tools recommended a more cost memory, fastutil.

It contains most of the realization of JKD collection, and more provincial memory.

Tips

On the basis of the above three principles, two tips.

  • Time with long / int representation without Date or String.

  • If the short string could be exhaustive or into ascii expressed, or can be represented by long int.

Note: Tips with specific scenarios related data can be aggressive optimization to save memory according to the actual situation.

to sum up

Performance and readability has always been a bit contradictory, here too, in order to save memory, had to choose, the code some ugly, some poor readability, but fortunately able to save some memory.

The above principle when really needed to save memory, may wish to try!

 

 

·END·

Programmers growth path

Although the road is far, certainly the line to

This paper originating in the "road programmer growth of" micro-channel public number of the same name, reply "1024" you know, give a praise chant.

Reply [520] receive the best programmers learning

Reply to [256] View Java programmers growth plan

 

Past wonderful review

 

 

 

Guess you like

Origin www.cnblogs.com/gdjk/p/10978904.html