Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does class without data member have memory footprint in java?

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 ?

like image 370
Silent Warrior Avatar asked Jun 12 '10 05:06

Silent Warrior


3 Answers

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.

like image 94
Donal Fellows Avatar answered Sep 21 '22 18:09

Donal Fellows


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

like image 33
Tony the Pony Avatar answered Sep 19 '22 18:09

Tony the Pony


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.

like image 29
dty Avatar answered Sep 17 '22 18:09

dty