tags:

views:

489

answers:

7

What is the memory overhead of a function in a class?

For example,

Class A
{
 int a
}

Class B
{
 int a
 int foo(int);
}

So 100 instances of class A should be 80 bytes. What about 100 instances of class B?

A: 

Probably sizeof the v-table pointer rounded up to a multiple of 16.

tpdi
Classes of both objects will have some sort of reference to their class.
Tom Hawtin - tackline
Yeah, the pointer to the table of virtual functions.
tpdi
+1  A: 

The method is not stored in the serialized form. It's as simple as that.

Edit: As for objects in the VM during execution, the size should be constant, regardless of how many methods the class of the object has.

gustafc
+3  A: 

80 bytes since a method is not included in the object. Unless you are also talking about the "vtable" type of thing, in which case probably 160 bytes.

Clarification on the 160 bytes. The 160 would be if each object allocated its own vtable (which is one possible implementation). Alternatively (and as is pointed out in the comments) a better way would be one vtable per class, which would mean 80 + the size of the pointer to the vtable (probably 4 or 8 bytes depending on the VM). So 84 or 88 bytes.

This whole thing totally depends on the way the VM allocates memory and deals with non-final methods. Without knowing how the particular VM is implemented neither question can be answered correctly.

TofuBeer
Both classes have some kind of reference to their class (an equivalent to a C++ vtbl pointer). It's not like each object is going to carry around loads of class information on any kind of reasonable VM.
Tom Hawtin - tackline
That is a VM specific implementation, as it is a C++ compiler specific imiplementation.
TofuBeer
Tom, you seem to be overly Sun implementation centric when you comment on my answers. If you cannot bother thinking that there are other possible VM implementations, well, I don't know what to say.
TofuBeer
+1  A: 

Equal, 80! ( in short)

aJ
+1  A: 

As a few other people at least have said, the method is not stored in the serialization.

You can easily do a test to show this in this example with the following code.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class SerializationTest {

 public static void main(String[] args) {
  serialize(true);
  serialize(false);
 }

 public static void serialize(boolean aOrB) {
  FileOutputStream fos = null;
  ObjectOutputStream out = null;
  try {
   fos = new FileOutputStream("output.txt");
   out = new ObjectOutputStream(fos);
   out.writeObject(aOrB ? new A() : new B());
   out.close();
   fos.close();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
  File file = new File("output.txt");
  System.out.println(file.length());
 }

 public static class A implements Serializable {
  int a = 0;
 }

 public static class B implements Serializable {
  int a = 0;

  public int foo(int a) {
   return a;
  }
 }
}

For me this prints out

48
48
PintSizedCat
Serialization does not necessarily reflect the way the data is allocated inside a running VM however.
TofuBeer
+2  A: 

The overhead is... none.

The method definition and code address are stored in the Class object, which has a unique instance toward which every instance of your object points. Since that would be the case, whether or not you added that method, the overhead for each individual object is nothing.

Varkhan
And I don't understand the down vote, since the answer is exact.
Varkhan
A: 

Objects of both classes will use about 20-24 bytes for 32/64-bit JVMs.

Peter Lawrey