What is the size of an empty class in C++ and Java?
Why is it not zero?
sizeof();
returns 1 in the case of C++.
8 Answers
Short Answer for C++:
The C++ standard explicitly says that a class can not have zero size.
Long Answer for C++:
Because each object needs to have a unique address (also defined in the standard) you can't really have zero sized objects.
Imagine an array of zero sized objects. Because they have zero size they would all line up on the same address location. So it is easier to say that objects can not have zero size.
Note:
Even though an object has a non zero size, if it actually takes up zero room it does not need to increase the size of derived class:
Example:
#include <iostream>
class A {};
class B {};
class C: public A, B {};
int main()
{
std::cout << sizeof(A) << "\n";
std::cout << sizeof(B) << "\n";
std::cout << sizeof(C) << "\n"; // Result is not 3 as intuitively expected.
}
g++ ty.cpp
./a.out
1
1
1

- 305,947
- 44
- 307
- 483

- 257,169
- 86
- 333
- 562
In the Java case:
- There is no simple way to find out how much memory an object occupies in Java; i.e. there is no
sizeof
operator. - There are a few ways (e.g. using
Instrumentation
or 3rd party libraries) that will give you a number, but the meaning is nuanced1; see In Java, what is the best way to determine the size of an object? - The size of an object (empty or non-empty) is platform specific.
The size of an instance of an "empty class" (i.e. java.lang.Object
) is not zero because the instance has implicit state associated with it. For instance, state is needed:
- so that the object can function as a primitive lock,
- to represent its identity hashcode,
- to indicate if the object has been finalized,
- to refer to the object's runtime class,
- to hold the object's GC mark bits,
- and so on.
Current Hotspot JVMs use clever tricks to represent the state in an object header that occupies two 32 bit words. (This expands in some circumstances; e.g. when a primitive lock is actually used, or after identityHashCode()
is called.)
1 - For example, does the size of the string object created by new String("hello")
include the size of that backing array that holds the characters? From the JVM perspective, that array is a separate object!

- 698,415
- 94
- 811
- 1,216
Because every C++ object needs to have a separate address, it isn't possible to have a class with zero size (other than some special cases related to base classes). There is more information in C++: What is the size of an object of an empty class? .

- 1
- 1

- 30,161
- 7
- 76
- 78
Because an object has to have an address in memory, and to have an address in memory, it has to occupy "some" memory. So, it is usually, in C++, the smallest possible amount, i.e. 1 char (but that might depend on the compiler). In Java, I wouldn't be so sure.. it might have some default data (more than just a placeholder like in C++), but it would be surprising if it was much more than in C++.

- 18,174
- 6
- 36
- 52
C++ requires that a normal instantiation of it have a size of at least 1 (could be larger, though I don't know of a compiler that does that). It allows, however, an "empty base class optimization", so even though the class has a minimum size of 1, when it's used as a base class it does not have to add anything to the size of the derived class.
I'd guess Java probably does pretty much the same. The reason C++ requires a size of at least 1 is that it requires each object to be unique. Consider, for example, an array of objects with size zero. All the objects would be at the same address, so you'd really only have one object. Allowing it to be zero sounds like a recipe for problems...

- 476,176
- 80
- 629
- 1,111
-
1I am sure (ok sort of speculating) at some point we will get them having a larger size to help with alignment or something like that. – Martin York Jan 25 '11 at 04:24
-
@Martin: Alignment only matters when you're going to access the memory -- but for a (conceptually) zero-sized object, there's nothing to access. About the only reason I can think of for it to happen is an implementation (e.g., Windows CE) where *everything* has a size of at least 2 and it's just more convenient to avoid an exception for this case (but that's not a conforming implementation, so it's kind of irrelevant as far as standard C++ goes). – Jerry Coffin Jan 25 '11 at 04:36
-
When I first started to learn C++ several years ago, it was with an old version of the Borland C++ compiler, and I did verify the size of an empty class, and it was 4 bytes. Since then, I always assumed it was 4 bytes (or whatever is the native size of the platform), but I never understood why it couldn't just be 1 (since it's never used). I guess one learns something everyday in C++. – Mikael Persson Jan 25 '11 at 04:50
-
The standard requires a non-zero size, but does not dictate the size. I can imagine that in platforms where pointers are required to hold some specific alignment, then that alignment would be the size of the empty object, to be able to hold unique pointers to the elements. In current intel architectures, that is not an issue (you can have a pointer to any address in memory), and because there will be no real access to the memory, the compiler does not need to apply the alignment for performance reasons. – David Rodríguez - dribeas Jan 25 '11 at 09:23
-
@dribeas: good point -- now that you mention it, on the 68000, even attempting to load an odd address into an address register would cause an exception, so I can see where it would make sense for it to use a size of 2 for an empty class. Early RISCs were similar, but most required a multiple of 4 instead... – Jerry Coffin Jan 25 '11 at 15:09
It's defined by the C++ standard as "a nonzero value", because an allocated object must have a nonzero size in order to have a distinct address. A class that inherits from an empty class, however, is not required to increase in size, barring the usual increase of a vtable if there are virtual functions involved.

- 53,300
- 8
- 96
- 166
I don't know if there is a sizeof() operator in java. What you can do is create an instance of the empty class (have it serializable), send it through a PipedOutputStream and read it as byte array - byteArray.length gives you the size.
Alternatively, write out the instance to a file using DataOutputStream, close the File, open it and file.length() will give you the size of the Object. Hope this helps, - M.S.

- 3,573
- 5
- 25
- 27
As others have pointed out, C++ objects cannot have zero size. Classes can have zero size only when they act as a subclass of a different class. Take a look at @Martin York's answer for a description with examples --and also look and vote the other answers that are correct to this respect.
In Java, in the hotspot VM, there is a memory overhead of 2 machine-words (usually 4 bytes in a 32 arch per word) per object to hold book keeping information together with runtime type information. For arrays a third word is required to hold the size. Other implementations can take a different amount of memory (the classic Java VM, according to the same reference took 3 words per object)

- 1
- 1

- 204,818
- 23
- 294
- 489