> 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));
}
}
}
>
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)
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.