how much memory does an instance of a class really take?

hello, say i have a simple class with some member variables and some functions.


class myClass {

 float a,b,c,d;

 myClass() {...}

 void X() { ...}
 void Y() { ...}
 void Z() { ...}
}

now when i create an instance of this class by doing
myClass instance = new myClass();

how much memory does this instance take up on the heap? it should be at least 16Bytes cause of the 4 floats. but how much more? thanks!

Using Unsafe and messing around with pointers in an array a bit, i noticed this:

Vec3[] vecs = new Vec3[n];
*n = n.length
*(n+4) = pointer to object 1
*(n+8) = pointer to object 2
etc

Now the intersting thing was that the pointers were the field-byte-count (2x int = 8 bytes) + 8

the object-data started after the first 8 bytes.

So, in my 32bit system, each object had an overhead of 8 bytes…

[quote=“Riven,post:2,topic:28815”]
Most JVMs that I know of have a 2-word object header used for pointers to class metadata, synchronization bits, etc… 2 words on a 32-bit system = 8 bytes. Practically speaking, it’s extremely memory inefficient to store large data sets of small objects as full-blown Java objects. Generally, you’ll see significant performance impacts due to GC, too.

A real world example that I’ve just been seing at work, an instance of java.lang.Thread is 96 bytes. Obviously a thread has references to other things, so that is for those too, not just the class information.

Endolf

The stack-size of the thread is way more significant, usually 1MB

Aye, but thats not part of the Thread class bytes, thats just something it uses :slight_smile:

It’s hard to tell exactly how many bytes a new Thread() will allocate, but the thread object itself is 96 :slight_smile:

Endolf

Hello,

Java 5 comes along with a feature called “Instrumentation”.
Instrumentation allows you to access a Instrumentation-mechanism of the JVM (using a Java Agent) to gather additional information about the jvm itself such as which classes are currently loaded into the JVM, what is the platform specific object size for a specific instance (at least an
estimation) and much more…

here is a short example:

First we have to define a Class that we can load as Agent.
This class has to define a method called premain with the args shown below.


/**
 *
 */
package de.tutorials;

import java.lang.instrument.Instrumentation;

/**
 * @author Tom
 *
 */
public class SimpleJavaAgent {
   
    private static Instrumentation instrumentation;
   
    public static void premain(String agentArgs, Instrumentation inst){
        instrumentation = inst;
    }

    public static Instrumentation getInstrumentation() {
        return instrumentation;
    }
}

After we compiled this class we have to jar it up using this special Manifest.mf File:


Manifest-Version: 1.0
Premain-Class: de.tutorials.SimpleJavaAgent
Can-Redefine-Classes: true

Here is an example how we could use our JavaAgent to access the JVM
Instrumentation:


/**
 *
 */
package de.tutorials;

/**
 * @author Tom
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        Class[] loadedClasses = SimpleJavaAgent.getInstrumentation()
                .getAllLoadedClasses();
        for (Class clazz : loadedClasses) {
            System.out.println(clazz);
        }

        System.out.println("Object sizes...");
        Object o = new Object();
        System.out.println(SimpleJavaAgent.getInstrumentation()
                .getObjectSize(o));
       
        Object oo = new Object() {
            Object o;
        };
        
System.out.println(SimpleJavaAgent.getInstrumentation().getObjectSize(
                oo));
    }
}

One can start this example using the java launcher in this way:
java -cp bin -javaagent:lib/simpleAgent.jar de.tutorials.Main

The Output is:


class sun.nio.cs.Surrogate
class java.util.Collections$ReverseComparator
class sun.instrument.TransformerManager
class java.nio.charset.CharsetEncoder
....
class sun.net.www.URLConnection
class [Z
class [B
class [C
class [I
class [S
class [J
class [F
class [D
Object sizes...
8
16

Sorry for my crappy english…

Kind regards and schönen Gruß
Tom