from http://www.javaspecialists.eu/archive/Issue029.html, http://www.javaspecialists.eu/archive/Issue142.html
| Цитата | The amount of memory can be determined by looking at the data members of the class and all the superclasses' data members. The algorithm I use works as follows:
1. The class takes up at least 8 bytes. So, if you say new Object(); you will allocate 8 bytes on the heap. 2. Each data member takes up 4 bytes, except for long and double which take up 8 bytes. Even if the data member is a byte, it will still take up 4 bytes! In addition, the amount of memory used is increased in 8 byte blocks. So, if you have a class that contains one byte it will take up 8 bytes for the class and 8 bytes for the data, totalling 16 bytes (groan!). 3. Arrays are a bit more clever, at least smaller primitives get packed. I'll deal with these later.
|
| Код | class A { boolean b1, b2, b3; }
|
будет занимать 24 байта = 8 (от Object ) + 3 * 4 (члены класса) = 20 байт + 4 байт (чтобы было кратно 8) |