Why does creating a large Java array consume so much memory?

Why next line

Object[] objects = new Object[10000000]; 

leads to a lot of memory (~ 40M) used by the JVM? Is there a way to find out the internal operation of a virtual machine when distributing arrays?

+4
source share
10 answers

Well, this allocates enough space for 10,000,000 links, as well as a small amount of overhead for the array object itself.

The actual size will depend on the VM, but it is not surprising that it takes up a lot of memory ... I would expect at least 40 MB and possibly 80 MB on a 64-bit virtual machine, unless it uses compressed oops for arrays.

Of course, if you populate an array with many different objects, it will take a lot more memory ... but the array itself still needs reference-only space.

+22
source

What do you mean by "big memory"? You allocate 10,000,000 pointers, each of which occupies 4 bytes (on a 32-bit machine) - this is about 40 MB of memory.

+17
source

You create ten million object references. The link is at least 4 bytes; IIRC in Java it may be 8, but I'm not sure about that.

Thus, with one line, you create 40 or 80 megabytes of data.

+3
source

You reserve space for ten million links. This is quite a bit.

+3
source

This leads to the fact that most of the memory is used, because it needs to allocate a lot of space for 10 million objects and related service data.

In order to look at the internal workings in the Java virtual machine, you can check your source code , as it is open source.

+3
source

Your array should store 10 million references to objects, which on modern platforms are 64-bit (8 byte) pointers. Since it stands out as a continuous piece of storage, it must accept 80 million bytes. This is large in one sense, small compared to the likely amount of memory that you have. Why is this bothering you?

+3
source

It creates an array with pointers of pointers 10.000.000, all are initialized with null .

What did you expect to say this is a lot?


Further reading

+3
source

One of the main reasons that arrays are widely used is that their elements can be accessed at a constant time. This means that the time required to access [i] is the same for each index i. This is because the address [i] can be determined arithmetically by adding a suitable offset to the address of the head of the array. The reason is that the space for the contents of the array is allocated as a contiguous block of memory.

+2
source

According to this site, memory usage for arrays is a 12-byte header + 4 bytes per element. If you declare an empty Object array containing 10M elements, then you have approximately 40 MB of memory used from the very beginning. If you start filling this array with a 10M object, the size increases quite quickly.

From this site, and I just tested it on my 64-bit machine, the size of a simple Object is about 31 bytes, so an array of 10M Object is about 12 bytes + (4 + 31 bytes) * 10M = 350,000 012 bytes (or 345 , 78 MB)

If your array contains objects of a different type, the size will be even larger.

I would advise you to use some kind of random access file to store data if you need to store so much data inside your program. Or even use a database like Apache Derby , which will also allow you to sort and filter your data, etc.

+2
source

I can lag behind time, but from the book Practical Java I realized that vectors are more efficient and faster than arrays. Is it possible to use a vector instead of an array?

0
source

All Articles