views:

140

answers:

4

My question is related to memory footprint in java for class without data member. Suppose in java I have a class which doesn't have data member and it only contains methods. So if I am creating instance of particular class then does it occupies memory in primary memory except object reference memory ?

+4  A: 

Yes, it does, because at minimum even an "empty" object has a pointer to its type information.

Jen
You are right. That's why I had mentioned in question that except Object reference/pointer. So apart from reference/point does it have any memory footprint ?
Silent Warrior
Depends on the JVM implementation. There will likely be some sort of a per-object synchronization mechanism, or marker flags for the garbage collector. My best guess would be at least 8 bytes of per-object overhead on a 32-bit machine.
Jen
The overhead can be up to 16 bytes, for a 32 bit JVM ... depending on the JVM implementation details. The overheads include the primitive object size, the link to the class, the representation of the primitive mutex, the representation of the identity hashcode, the flags for finalization and so on.
Stephen C
@Stephen: The default identity hashcode is derived from `this`, the primitive object size is known from the type, and finalization handling is kept outside the object (i.e., finding out whether an object is being finalized is relatively expensive if you're not the memory manager).
Donal Fellows
The identity hashmap can't be computed from this otherwise it would change when GC occurred and the heap was compacted
dty
@Donal - see @Danny's answer about identity hashcode. Some JDKs store it in the object header. Others reserve 2 flag bits and store the identity hashcode at the end of the object.
Stephen C
@Donal - there is a flag that says if an object has *ever* been finalized. If you don't keep this in the object header, then you need a separate data structure for this flag.
Stephen C
@Stephen: Thinking about it, they're more likely to keep the identity in the object than the synchronization machinery; the latter can be kept elsewhere and keyed off the identity, since that is not needed by most objects. (That would also explain why the `synchronized` keyword is not recommended in high-performance code.) The finalization bit can be packed in with the class reference, whose lower two or three bits would otherwise be always zero. In any case, that still looks like a two-word minimum.
Donal Fellows
+3  A: 

Let's be clear. References TO your object will, of course, take space. But your object will also take space for its 'this' pointer (i.e. so you can distinguish different instances) and also for the fields of any superclasses - e.g. Object - and finally whatever overhead the heap's internal datastructures have.

dty
+2  A: 

Benchmarking memory is difficult because of various sources of interference (growing heap, GC), but it is still worth a try:

public static void main(String[] args)
{
    final int n = 1000000;

    final Runtime runtime = Runtime.getRuntime();

    final Object[] objects = new Object[n];

    final long memory0 = runtime.totalMemory() - runtime.freeMemory();

    for (int i = 0; i < objects.length; i++)
    {
        objects[i] = new Object();
    }

    final long memory1 = runtime.totalMemory() - runtime.freeMemory();

    final long memory = memory1 - memory0;

    System.out.printf(
        "%s %s\n",
        System.getProperty("java.vm.name"),
        System.getProperty("java.vm.version"));

    System.out.printf("%d %d %.1f\n", n, memory, 1.0 * memory / n);
}

Java HotSpot(TM) Server VM 14.3-b01 1000000 8000336 8.0

starblue
+4  A: 

Ultimately, every Java object know its class and has a synchronization primitive optionally attached to it (though this can be synthetic). That's two references which it is difficult to make a java.lang.Object instance do without. Everything else derives from that class, so you've got a floor for costs which worked out to be at least 8 bytes in Java 1.3.1. A profiler will tell you current costs if you really need them.

Donal Fellows
+1 for the very good link
Arne
A modern JVM holds the object monitor data in unused bits of other pointers that it holds (e.g. the bottom two bits of the class pointer)
dty
@Danny: It's got three bits available; “modern” memory managers align on 8-byte boundaries (when not working with arrays, which are denser). And by “modern” I do mean *everything* for 10 years or more.
Donal Fellows