ApeX said:
Is there a way to get size of an object stored in memory?
I have an xmldocument which i fill with the some data, and just before
i call the Save method, i wanna get
the number of bytes that xmldocument is using in memory?
It's impossible to give an exact amount for that.
Consider this example:
string[] x = {
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
"This is a string value containing quite some characters.",
};
You would think that the array would use about 1300 bytes, but it
doesn't. The array doesn't contain ten references to ten separate
strings, it contains ten references to the same string, so it's using
about 160 bytes. Or, if you consider that the string is not created just
for the array but is a constant in the assembly, it's only using about
50 bytes.
(Memory sizes mentioned assuming a 32-bit system.)
So, you can't just add up what an object references and say that this is
the size. Also it depends on if you mean:
1. The amount of memory that is used to hold everyting for the object.
2. The amount of memory saved if you didn't create the object.
3. The amount of memory needed to create the object.
4. The amount of memory needed to create another copy of the object.
Those can all end up being different amounts...