Java memory usage

Source: Internet
Author: User
Tags float double
I have made some optimizations on the memory usage of the program recently, and have achieved good results. I have summarized some experiences.
I believe it will be helpful for you to write high-quality programs.
The following discussion is not applicable to 64-bit systems for 32-bit systems.

You often write a program, test it, and have no function problems. When you check that the memory usage is not large, you don't need to consider other things. However, the program may use a data structure. When the data size increases, the memory usage will surge.

The basic & key question is, How much memory does Java occupy ?????????

There are 8 primitive types
Byte short int long float double char Boolean their lengths are
1 2 4 8 4 8 2 1
For example
Long [] DATA = new long [1000];
Memory usage: 8*1000 bytes
In addition, data itself is an object and also occupies a certain amount of memory. In the post-explanation, of course, it is ignored for 8*1000.

Let's talk about the usage of objects. before talking about this, let's talk about references.
Java has no pointer and only references. References are safe.

This statement is correct, but in terms of mechanism, the reference is a pointer, but the JVM has many checks and restrictions on the usage of pointers, and this reference/pointer becomes very safe.

Conclusion: a single reference occupies 4 bytes in a 32-bit system.

Object OBJ = NULL; // 4 byte
Object [] objs = new object [1000]; // at least 4*1000 bytes

You can see that I have defined an OBJ or null, which occupies 4 bytes.
Defines an objs with 1000 elements, but all of them are null. Each of them occupies 4 bytes.
Yes !!!!!
Although OBJ = NULL, it is already a reference, or a pointer.
The pointer also occupies a place !!!! Ah !!!! Ah !!!!

Next, let's draw another conclusion: the object occupies 8 bytes. Note that the object is pure.

Object OBJ = new object (); // how many ????

8 byte ?? Error !! 12 byte. I forgot to add a reference. 8 byte is the object content.
Remember object OBJ = new object (); 12 bytes

Object [] objs = new object [1, 1000];
For (INT I = 0; I <1000; I ++ ){
Objs [I] = new object ();
}

Occupies at least 12*1000 bytes

Inference: the object occupies 12 bytes, which seems to be in conflict with the above conclusion ??!!
No !! No matter whether the object is recycled or not, it must be referenced by others?
Is there a pointer to it in general? Since the reference or pointer takes 4 bytes
The total value is 12 bytes. An object must contain at least 12 bytes.

I still come to the conclusion directly, and I have done everything in the process of deduction. Isn't it a dirty job to do it !!
One integer occupies 16 bytes

In this case, you may have questions. Integer = Object + int is:
Public class INTEGER {
Public int value;
}
Integer should occupy 8 + 4 = 12 bytes.
What you said makes sense, but JVM has limits on all objects !!
This restriction was discovered by me, that is, no matter what object occupies space, if it is a multiple of 8
12 is not a multiple of 8. It can only be 16 !!!

Inference: byte also occupies 16 bytes !!!!!!!!!!!

Q:
Byte [] bytes = new byte [1, 1000];
How much space does it occupy?
A: approximately (at least) (16 + 4) * 1000 bytes
Good Guy !!!!!!!!

Topic: how to calculate the array space usage?
I have come to the conclusion that it takes a longer time to deduce this:
For an array, the object of the array has a Length attribute, and the element of the array is equivalent to its member.
Public class Array {
Public int length;
//... Other members
}
For arrays, can we directly obtain the Length attribute?

Public byte [] bytes = new byte [1, 1000];
System. Out. println (bytes. Length); // check whether the Length attribute exists.
The above bytes conversion is as follows:
Public class Array {
Public int length;
Public byte byte0;
Public byte byte1;
...
Public byte byte999;
}
The memory occupied by bytes is as follows:
4 + [8 + 4 + 1*1000] = 4 + [1012] = 4 + 1016 = 1020
4 is the bytes reference, 8 is the base of the object, and 4 is the part of the Length attribute.
1000 accounts for 1000 members, which is originally 1012, but must be a multiple of 8 to 1016.
Total is 1020
Another example is:
Byte [] bytes = new byte [4];
The memory usage is:
4 + [8 + 4 + 4*1] = 4 + [16] = 20;

Byte [] bytes = new byte [3]; Also 20

The element is an array of objects and the object is considered as a member. (Note that only the space that references this array can be pushed to a common class)

Byte [] bytes = new byte [1, 1000];
The definition of this bytes is equivalent:
Public class Array {
Public int length;
Public byte byte0;
.....
Public byte byte999;
}
The occupied space is:
4 + [8 + 4 + 4*1000] + 16*1000 = 4 + 4016 + 16000 = Do It Yourself

Inference: Never use byte [] for 20 times !!!!!!!

You may not understand it all at once. It's Okay To think about it more. For a common class
The content occupation is the occupation of the base addition Member, and the object member only records the reference
Public Class ABC {
Public int N;
Public byte B;
Public object OBJ;
}
Its content usage is: [8 + 4 + 1 + 4] = 24
So ABC one = new ABC () occupies 4 + 24 = 28
Note: The member OBJ of ABC is not counted. If you want to calculate the value, you can loop through this process. (Think about it)

Example:

Public Class ABC {
Public byte B;
Public object OBJ = NULL;
}

Public class def {
Public int N;
Public byte B;
Public abc obj = new ABC ();
}
Q:
Def one = new def (); // What is the percentage?
A:
4 + [8 + 4 + 1 + 4] + [8 + 1 + 4] = 4 + 24 + 16 = 44

Public Class ABC {
Public byte B;
Public object OBJ = NULL;
}

Public class def {
Public int N;
Public byte B;
Public ABC [] objs = new ABC [100];
{
For (INT I = 0; I <10; I ++ ){
Objs [I] = new ABC ();
}
}
}
Q:
Def one = new def (); // What is the percentage?
A:
Kao, I can't even figure it out, but after I write a program, I can figure it out. If you give it an object, it can recursively calculate the total memory occupied, this program is not complex. You can also write it out. I will wait for the opportunity to release it again.

Let's talk about string separately. The structure of string is:
Public class string {
Private Final char value [];
Private Final int offset;
Private Final int count;
Private int hash; // default to 0
}
Therefore, the char [] occupies at least 8 + 4 + 4 + 4 + 4] = 24 bytes.
Add reference, a total of 28 bytes
So
String S = "";
Occupies 28 bytes !!!!! Although its length is 0
If the exact calculation is performed, plus the occupation of referencing a string is
4 + 24 + [8 + 4 + 2 * length]
String S = ""; occupied by 28 + 16 = 44
String S = "AB" occupies 28 + 16 = 44
String S = "ABC" occupies 28 + 24 = 52

To put it bluntly, string is a common class. In this case, string consumes a lot of memory, so JVM is optimized and the same content should be reused as much as possible. Therefore, apart from 28, the Char [] is probably the same
For example
String [] S = new string [1000];
For (INT I = 0; I <1000; I ++ ){
S [I] = new string ("abcdefasdjflksadjflkasdfj ");
}
The occupied size is 28*1000, and the 1000 strings themselves basically do not occupy the memory, only one !!!!!!
The string must be at least 28, or a maximum of 28 !!!!!!!!

It is important to compare the data structure occupied by memory:
Basically, it is the packaging of primitive.

Instance:
I used to use
Hashtable <string, integer> structure with 1 million Elements
After changing to string [] + int [], the memory usage has improved a lot and the speed is fast.
1 million of the string [] will be sorted quickly, which will take more than 2 seconds. It will take 2 minutes to search, which is about less than hash.

Complete!

Note:
1. The above conclusions apply to 32-bit systems. For 64-bit systems, there are many differences. The conclusion is that although the 64-bit system can use more content, the same program also occupies a lot of memory.
2. The above discussion is the memory occupied by the class instance, without considering the occupation of static variables. Static variables reference the class data. The usage of the content is irrelevant to the instance. You can calculate the content separately.
3. The above does not take into account the memory occupied by the class itself. The class itself also needs to take up some places, that is, the structure of the class and the occupation of static variable reference. However, the usage is static and does not increase as the number of instances increases. It is not easy to make statistics. If you want to make statistics, look at the jclass representation in the JVM source code.
4. Dear labor achievements of others, please note the author zms.
 

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.