Srikanth Adiga
Srikanth Adiga

Reputation:

What is the memory overhead of a Java method?

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?

Upvotes: 15

Views: 2614

Answers (7)

Peter Lawrey
Peter Lawrey

Reputation: 533442

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

Upvotes: 2

Varkhan
Varkhan

Reputation: 16751

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.

Upvotes: 11

TofuBeer
TofuBeer

Reputation: 61526

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.

Upvotes: 4

Henry B
Henry B

Reputation: 8047

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

Upvotes: 2

aJ.
aJ.

Reputation: 35450

Equal, 80! ( in short)

Upvotes: 1

gustafc
gustafc

Reputation: 28865

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.

Upvotes: 1

tpdi
tpdi

Reputation: 35141

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

Upvotes: -1

Related Questions