3

Suppose I have:

Class A{
   int a;
}

A obj = new A();

Then what will be size of obj? Will it be of same size as int size, like in C?

If I can figure out this, then I can keep large HashMap in RAM without using database.

Thanks in advance.

EDIT

Friends,

Actually I have:

HashMap<Long, List<T>> map;

and

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

And size of map may grow to have 10000000 keys and for each key I will have list of size 100-1000.

Will this whole map stay in heap?

EDIT 2

When I loaded map with around 70000 keys, and when I serialized it to file, file was of around 18 MB, so will my map be of 18 MB in heap?

7
  • Have you tried to print out the size of the object in a test program? Commented Dec 27, 2012 at 15:42
  • 1
    I believe that it is up to the JVM how much memory it needs for each instance of an object. You are still thinking too C-ish. Just trust the JVM. Commented Dec 27, 2012 at 15:42
  • @OdayMansour, how can I do sizeof in Java. That operator is in C only. Commented Dec 27, 2012 at 15:44
  • You could look at my question - there are some nice answers on how java manages to allocate memory. Commented Dec 27, 2012 at 15:44
  • @Philipp, so will JVM can use any size? I will have huge HashMap, and I have fear of Hear Shortage. Commented Dec 27, 2012 at 15:45

4 Answers 4

7

It may depend on JVM, but in practice, for 32-bit JVM, an object = 8-byte header + fields. Header consist of Id + reference to class.

Fields size depends on field type, reference - 8 bytes, boolean - 1 byte, etc

Besides objects are aligned to 8 bytes. That is your A takes 16 bytes. Minimum size = 8 bytes, no fields.

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

If you use Oracle JVM you can use sun.misc.Unsafe to investigate object structure, byte by byte.

Sign up to request clarification or add additional context in comments.

2 Comments

@Evgeniy, will it be possible to keep whole hashmap in heap, as I have said in edit?
@user1508907 I think it's more than in the file. Open HashMap source. K+V is stored in Entry, K(Long) 16 bytes; T - 24; Entry - 24; total - 64 bytes per entry (K,V)
0

No. It can never be of the same size as that in C because an object is created on heap whereas a reference variable is created on Stack. So therefore, it won't be of the same size.

Comments

0

it will be size of the Object plus size of the int , because of your private member.

"I can keep large HashMap in RAM without using database" just need to deal with persistentce: serialization and deserialization, and as Object stream it is be to much.

Comments

0

64 bit system is different to 31 bit system

64 bit system is different to 31 bit system

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.