How to know the size of an object of a class?

B

Bruce .J Sam

There is a simple program just want to show my question:
/* ************************************************************* */
class A {
int i;
}

class B {
int i;
int j;
}

class C {
int i;
int j;
void f() {
System.out.println("f()");
}
}

public Test {
public static void main (String [] args) {
A a = new A();
B b = new B();
C c = new C();
}
}
/* ************************************************************* */
In language C, I can use sizeof() to know the memory's size.Is there
any method to do like language C in Java?If not,how to know the size of
the object of the class A, B, C?Is it the size of object a is smaller
than object b, object b is smaller than object c?
 
J

Jason Herald

I am not totally sure of a method to tell the size of 1 specific object
however i found a great link that might help:
http://martin.nobilitas.com/java/sizeof.html

This has some info on how to calculate the memory used and also how to tell
the total memory used by the current process.

--
If you have any additional questions or need source code email
(e-mail address removed).

Thanks

Jason Herald
 
T

Tony Morris

Bruce .J Sam said:
There is a simple program just want to show my question:
/* ************************************************************* */
class A {
int i;
}

class B {
int i;
int j;
}

class C {
int i;
int j;
void f() {
System.out.println("f()");
}
}

public Test {
public static void main (String [] args) {
A a = new A();
B b = new B();
C c = new C();
}
}
/* ************************************************************* */
In language C, I can use sizeof() to know the memory's size.Is there
any method to do like language C in Java?If not,how to know the size of
the object of the class A, B, C?Is it the size of object a is smaller
than object b, object b is smaller than object c?

You can get an approximation of the memory used by an object from
java.lang.instrument.Instrumentation.
The API Specification for it didn't work for me, but here it is anyway:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html

Remember though, to want to get that kind of information raises questions
about whether you are doing things The Wrong Way(TM).
 
K

Kevin McMurtrie

Bruce .J Sam said:
There is a simple program just want to show my question:
/* ************************************************************* */
class A {
int i;
}

class B {
int i;
int j;
}

class C {
int i;
int j;
void f() {
System.out.println("f()");
}
}

public Test {
public static void main (String [] args) {
A a = new A();
B b = new B();
C c = new C();
}
}
/* ************************************************************* */
In language C, I can use sizeof() to know the memory's size.Is there
any method to do like language C in Java?If not,how to know the size of
the object of the class A, B, C?Is it the size of object a is smaller
than object b, object b is smaller than object c?

Java doesn't inline storage of Object member data. The size of an
object is always very small because it contains only references to
non-primitive data.

You can always use an interface to enforce implementation of a sizeof()
method. I've done that for complex caching.
 
I

iamfractal

Bruce .J Sam said:
There is a simple program just want to show my question:

El Snippoza.
}
/* ************************************************************* */
In language C, I can use sizeof() to know the memory's size.Is there
any method to do like language C in Java?If not,how to know the size of
the object of the class A, B, C?Is it the size of object a is smaller
than object b, object b is smaller than object c?

Hi, Bruce!

http://www.javapractices.com/Topic83.cjp

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top