Object size in Java

Suppose I have:

Class A{
   int a;
}

A obj = new A();

Then what is the size of obj? Is it the same size as int, just like in C?

If I can figure this out, I can keep large hashmaps in ram without using a database

Thank you in advance

edit

Friends,

In fact, I have:

HashMap<Long,List<T>> map;

and

class T{
   private int a;
   private int b;
   private int c;
   // constructor,getters and setters
}

And the size of the map may increase to 10000000 keys. For each key, I will have a list of sizes of 100-1000

Will the whole map stay in the pile?

Edit 2

When I load a map with about 70000 keys, when I serialize it into a file, the file is about 18 MB, so is it 18 MB in my map heap?

Solution

It may depend on the JVM, but in fact, for a 32 - bit JVM, the object = 8 - byte header field The title contains an ID reference to the class

The field size depends on the field type, reference – 8 bytes, Boolean – 1 byte, etc

Except that the object is aligned to 8 bytes That's your A. It takes 16 bytes Minimum size = 8 bytes, no fields

More here http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

If you are using an Oracle JVM, you can use sun misc. Unsafe investigates the object structure byte by byte

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>