Finding size of object

hi,I want a code which finds the Size of object in memory.And don't want to write extra empty argument constructor and other compromising stuff.I am srtogly thinking that there is no way, but still waiting for your comments.Please replay immediately,
[286 byte] By [santoshjavaa] at [2007-10-1 22:00:56]
# 1

> I want a code which finds the Size of object in in memory.

Why?

> And don't want to write extra empty argument

> constructor and other compromising stuff.

> I am strongly thinking that there is no way,

> but still waiting for your comments.

>

> Please replay immediately,

The word you are looking for is "reply"

and your request for people to reply is superfluous, if you did not want us to reply you would not have posted here...

Your request for people to reply immediately

is likely to cause you to have to wait longer for an answer...

(dictating when someone else should reply is considered rude).

You may be able to glean the information you are looking for from this sample codepublic final class Size {

public static final void main(String[] arg) {

Runtime rt = Runtime.getRuntime();

Object[] root = new Object[2];

for(int n=1;;n<<=1) {

Object[] tmp = root;

root = new Object[2];

root[0] = tmp;

root[1] = new Object[n];

long size = rt.totalMemory()-rt.freeMemory();

for(int k=0;k<n;++k) {

((Object[])root[1])[k] =

new Object(); // replace this

}

size = rt.totalMemory()-rt.freeMemory() - size;

System.out.println("Ratio "+(size/(double)n));

}

}

}

>

tschodta at 2007-7-13 8:05:08 > top of Java-index,Other Topics,Algorithms...
# 2
> Please replay immediately,Why do you want a reply immediately? It doesn't sound very polite.
prometheuzza at 2007-7-13 8:05:08 > top of Java-index,Other Topics,Algorithms...
# 3

And now, an immediate replay:

hi,

I want a code which finds the Size of object in in memory.

And don't want to write extra empty argument

constructor and other compromising stuff.

I am srtogly thinking that there is no way, but still waiting for your comments.

(remainder elided to prevent infinite loop)

pm_kirkhama at 2007-7-13 8:05:08 > top of Java-index,Other Topics,Algorithms...
# 4
> (remainder elided to prevent infinite loop)Thanks, or else we'd have a nasty StackOverflowException to catch.; )
prometheuzza at 2007-7-13 8:05:08 > top of Java-index,Other Topics,Algorithms...
# 5

Typically in Java size of object doesn't make much sense. Object may reuse other objects in unpredictable manner. The only thing you can do is to check (number of instances)/(number of unique instances) ratio and if it's something over 10-100 consider making object immutable and storing single copy for most common values.

RoboTacta at 2007-7-13 8:05:08 > top of Java-index,Other Topics,Algorithms...