Home All Groups Group Topic Archive Search About
Author
15 Dec 2008 8:50 PM
ApeX
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?

Thanx!

Author
15 Dec 2008 9:11 PM
Stanimir Stoyanov
Regarding the XmlDocument case, you can first save it to a MemoryStream and
retrieve its Length value. The stream will then be disposed automatically if
you use the 'using' syntax:

            using (MemoryStream stream = new MemoryStream())
            {
                        xmlDoc.Save(stream);

                        // Do something with stream.Length. You can also
write the stream bytes to your file instead of calling Save again.
            }
--
Stanimir Stoyanov
http://stoyanoff.info

Show quoteHide quote
"ApeX" <mmo***@gmail.com> wrote in message
news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...
> 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?
>
> Thanx!
Are all your drivers up to date? click for free checkup

Author
15 Dec 2008 9:21 PM
Nicholas Paldino [.NET/C# MVP]
Mind you, that this is the length of the representation of the info set
encoded as text, it is NOT the size of the object.  The document has
internal structures that it is using to store the infoset in memory and it's
not directly analagous to the representation of the infoset in text.

    I assume that the length of the infoset encoded as text is what the OP
wanted, but the difference is worth pointing out, IMO.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
news:10ABCB4E-8DEC-4E80-B6BB-BE7C0FBAA400@microsoft.com...
> Regarding the XmlDocument case, you can first save it to a MemoryStream
> and retrieve its Length value. The stream will then be disposed
> automatically if you use the 'using' syntax:
>
>            using (MemoryStream stream = new MemoryStream())
>            {
>                        xmlDoc.Save(stream);
>
>                        // Do something with stream.Length. You can also
> write the stream bytes to your file instead of calling Save again.
>            }
> --
> Stanimir Stoyanov
> http://stoyanoff.info
>
> "ApeX" <mmo***@gmail.com> wrote in message
> news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...
>> 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?
>>
>> Thanx!
>
Author
15 Dec 2008 9:46 PM
Stanimir Stoyanov
I agree, Nicholas, I described this in my second reply to the OP. In this
case, there is no (easy?) way to determine the amount of memory occupied by
a given object of reference type.
--
Stanimir Stoyanov
http://stoyanoff.info

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:%23aC6gsvXJHA.6064@TK2MSFTNGP05.phx.gbl...
>    Mind you, that this is the length of the representation of the info set
> encoded as text, it is NOT the size of the object.  The document has
> internal structures that it is using to store the infoset in memory and
> it's not directly analagous to the representation of the infoset in text.
>
>    I assume that the length of the infoset encoded as text is what the OP
> wanted, but the difference is worth pointing out, IMO.
>
>
> --
>          - Nicholas Paldino [.NET/C# MVP]
>          - mvp@spam.guard.caspershouse.com
>
> "Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
> news:10ABCB4E-8DEC-4E80-B6BB-BE7C0FBAA400@microsoft.com...
>> Regarding the XmlDocument case, you can first save it to a MemoryStream
>> and retrieve its Length value. The stream will then be disposed
>> automatically if you use the 'using' syntax:
>>
>>            using (MemoryStream stream = new MemoryStream())
>>            {
>>                        xmlDoc.Save(stream);
>>
>>                        // Do something with stream.Length. You can also
>> write the stream bytes to your file instead of calling Save again.
>>            }
>> --
>> Stanimir Stoyanov
>> http://stoyanoff.info
>>
>> "ApeX" <mmo***@gmail.com> wrote in message
>> news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...
>>> 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?
>>>
>>> Thanx!
>>
>
>
Author
15 Dec 2008 11:25 PM
Nicholas Paldino [.NET/C# MVP]
Stanimir,

    Without hooking into the CLR, there doesn't seem to be an easy way to do
this.

    Curious, for what purpose do you want to see this?

--
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
news:EA2C4205-5BED-4AEE-BFA3-DDC98BE1D7CF@microsoft.com...
>I agree, Nicholas, I described this in my second reply to the OP. In this
>case, there is no (easy?) way to determine the amount of memory occupied by
>a given object of reference type.
> --
> Stanimir Stoyanov
> http://stoyanoff.info
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote
> in message news:%23aC6gsvXJHA.6064@TK2MSFTNGP05.phx.gbl...
>>    Mind you, that this is the length of the representation of the info
>> set encoded as text, it is NOT the size of the object.  The document has
>> internal structures that it is using to store the infoset in memory and
>> it's not directly analagous to the representation of the infoset in text.
>>
>>    I assume that the length of the infoset encoded as text is what the OP
>> wanted, but the difference is worth pointing out, IMO.
>>
>>
>> --
>>          - Nicholas Paldino [.NET/C# MVP]
>>          - mvp@spam.guard.caspershouse.com
>>
>> "Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
>> news:10ABCB4E-8DEC-4E80-B6BB-BE7C0FBAA400@microsoft.com...
>>> Regarding the XmlDocument case, you can first save it to a MemoryStream
>>> and retrieve its Length value. The stream will then be disposed
>>> automatically if you use the 'using' syntax:
>>>
>>>            using (MemoryStream stream = new MemoryStream())
>>>            {
>>>                        xmlDoc.Save(stream);
>>>
>>>                        // Do something with stream.Length. You can also
>>> write the stream bytes to your file instead of calling Save again.
>>>            }
>>> --
>>> Stanimir Stoyanov
>>> http://stoyanoff.info
>>>
>>> "ApeX" <mmo***@gmail.com> wrote in message
>>> news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...
>>>> 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?
>>>>
>>>> Thanx!
>>>
>>
>>
>
Author
16 Dec 2008 8:37 AM
ApeX
>  Curious, for what purpose do you want to see this?

I have a datatable which through i loop, and add data to the
xmldocument accordingly. The problem is that the max size of the
xmldocument must be 500kb, and if it's larger than that then the file
must be broken into several files. Another problem is that each new
created file must be well xml formatted.

So, i need to find out when the file is exceeding 500kb, and then
write a file with the processed data, and then make another file or
files with the remaining data if it doesn't exceed 500kb, and etc.
etc..
Author
16 Dec 2008 9:04 AM
Göran_Andersson
ApeX wrote:
>>  Curious, for what purpose do you want to see this?
>
> I have a datatable which through i loop, and add data to the
> xmldocument accordingly. The problem is that the max size of the
> xmldocument must be 500kb, and if it's larger than that then the file
> must be broken into several files. Another problem is that each new
> created file must be well xml formatted.
>
> So, i need to find out when the file is exceeding 500kb, and then
> write a file with the processed data, and then make another file or
> files with the remaining data if it doesn't exceed 500kb, and etc.
> etc..
>

Then you were not asking for what you wanted.

The amount of memory that the XmlDocument is using is not the length of
the file when you write it out as XML. The elements are stored
completely different in memory.

Have a look at the OuterXml property of the XmlNode object.

--
Göran Andersson
_____
http://www.guffa.com
Author
16 Dec 2008 11:11 AM
Manu
On Dec 16, 2:04 pm, Göran Andersson <gu***@guffa.com> wrote:
Show quoteHide quote
> ApeX wrote:
> >>  Curious, for what purpose do you want to see this?
>
> > I have a datatable which through i loop, and add data to the
> > xmldocument accordingly. The problem is that the max size of the
> > xmldocument must be 500kb, and if it's larger than that then the file
> > must be broken into several files. Another problem is that each new
> > created file must be well xml formatted.
>
> > So, i need to find out when the file is exceeding 500kb, and then
> > write a file with the processed data, and then make another file or
> > files with the remaining data if it doesn't exceed 500kb, and etc.
> > etc..
>
> Then you were not asking for what you wanted.
>
> The amount of memory that the XmlDocument is using is not the length of
> the file when you write it out as XML. The elements are stored
> completely different in memory.
>
> Have a look at the OuterXml property of the XmlNode object.
>
> --
> Göran Andersson
> _____http://www.guffa.com

But a very interesting discussion was started by this topic.
Author
16 Dec 2008 5:01 PM
Göran_Andersson
Manu wrote:
> But a very interesting discussion was started by this topic.

Yes, moot discussons are always interresting, otherwise they would not
exist in the first place. :)

--
Göran Andersson
_____
http://www.guffa.com
Author
16 Dec 2008 12:12 PM
Stanimir Stoyanov
The length of the OuterXml string will not necessarily reflect the length of
the data *on disk* depending on the encoding used when saving.

I think it would be a better idea to save the xml document to a MemoryStream
and then check its length, as proposed in my first reply to the OP.
--
Stanimir Stoyanov
http://stoyanoff.info

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:%23uFcb11XJHA.6000@TK2MSFTNGP05.phx.gbl...
> ApeX wrote:
>>>  Curious, for what purpose do you want to see this?
>>
>> I have a datatable which through i loop, and add data to the
>> xmldocument accordingly. The problem is that the max size of the
>> xmldocument must be 500kb, and if it's larger than that then the file
>> must be broken into several files. Another problem is that each new
>> created file must be well xml formatted.
>>
>> So, i need to find out when the file is exceeding 500kb, and then
>> write a file with the processed data, and then make another file or
>> files with the remaining data if it doesn't exceed 500kb, and etc.
>> etc..
>>
>
> Then you were not asking for what you wanted.
>
> The amount of memory that the XmlDocument is using is not the length of
> the file when you write it out as XML. The elements are stored completely
> different in memory.
>
> Have a look at the OuterXml property of the XmlNode object.
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
Author
16 Dec 2008 12:27 PM
Anthony Jones
"Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
news:7F78CCB2-E9E0-4FB2-BE1E-8C30E24DCFC7@microsoft.com...
> The length of the OuterXml string will not necessarily reflect the length
> of the data *on disk* depending on the encoding used when saving.
>
> I think it would be a better idea to save the xml document to a
> MemoryStream and then check its length, as proposed in my first reply to
> the OP.

A far better approach would be to ditch XmlDocument altogether.

Create XmlTextWriter on a BufferedStream on a FileStream.

Use XmlTextWriter to create the Xml as needed which will just stream out the
destination file, the BufferedStream Length property would tell you exacly
how much has been written to the file.

When appropriate the existing content can be closed off and a new pair of
streams and a writer could be created to continue with the next traunch of
content and so on.

This approach allows you to accurately monitor the size of XML file being
created, doesn't require frequent large strings to be generated and dropped
(as using OuterXml.Length would do) and limits the demand for memory in the
application (memory requirements limited to the size of buffer choosen when
creating the BufferedStream).

--
Anthony Jones - MVP ASP/ASP.NET
Author
16 Dec 2008 4:04 PM
not_a_commie
> > I think it would be a better idea to save the xml document to a
> > MemoryStream and then check its length, as proposed in my first reply to
> > the OP.
>
> A far better approach would be to ditch XmlDocument altogether.
>
> Create XmlTextWriter on a BufferedStream on a FileStream.

I'm not sure you need to ditch the XmlDocument object. However, I
absolutely agree that a MemoryStream is almost never what you want
when dealing with XML. It will add byte markers to the start of the
stream. You should write out your documents this way:

using (var writer = new XmlTextWriter(filename, new UTF8Encoding
(false)))
{
    writer.Formatting = Formatting.Indented;
    xmlDoc.Save(writer);
    writer.Close();
}
Author
16 Dec 2008 9:16 PM
Anthony Jones
Show quote Hide quote
"not_a_commie" <notacom***@gmail.com> wrote in message
news:042e7171-5dae-43e4-9611-e253c7127746@p2g2000prf.googlegroups.com...
>> > I think it would be a better idea to save the xml document to a
>> > MemoryStream and then check its length, as proposed in my first reply
>> > to
>> > the OP.
>>
>> A far better approach would be to ditch XmlDocument altogether.
>>
>> Create XmlTextWriter on a BufferedStream on a FileStream.
>
> I'm not sure you need to ditch the XmlDocument object. However, I
> absolutely agree that a MemoryStream is almost never what you want
> when dealing with XML. It will add byte markers to the start of the
> stream. You should write out your documents this way:
>
> using (var writer = new XmlTextWriter(filename, new UTF8Encoding
> (false)))
> {
> writer.Formatting = Formatting.Indented;
> xmlDoc.Save(writer);
> writer.Close();
> }

At what point would the OP know that  the doc needed save and another
started?

We tend to stick with XmlDocument because we're comfortable with it and its
more flexiable than the forward only creation style needed when using an
XmlTextWriter directly.  In many situations, though, the XmlTextWriter can
be used with no loss of functionality and its much more efficient.  In this
special case it actual provides us with a function thats very difficult to
achieve with XmlDocument.

--
Anthony Jones - MVP ASP/ASP.NET
Author
17 Dec 2008 1:41 AM
Göran_Andersson
Anthony Jones wrote:
Show quoteHide quote
>
> "Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
> news:7F78CCB2-E9E0-4FB2-BE1E-8C30E24DCFC7@microsoft.com...
>> The length of the OuterXml string will not necessarily reflect the
>> length of the data *on disk* depending on the encoding used when saving.
>>
>> I think it would be a better idea to save the xml document to a
>> MemoryStream and then check its length, as proposed in my first reply
>> to the OP.
>
> A far better approach would be to ditch XmlDocument altogether.
>
> Create XmlTextWriter on a BufferedStream on a FileStream.
>
> Use XmlTextWriter to create the Xml as needed which will just stream out
> the destination file, the BufferedStream Length property would tell you
> exacly how much has been written to the file.
>
> When appropriate the existing content can be closed off and a new pair
> of streams and a writer could be created to continue with the next
> traunch of content and so on.
>
> This approach allows you to accurately monitor the size of XML file
> being created, doesn't require frequent large strings to be generated
> and dropped (as using OuterXml.Length would do) and limits the demand
> for memory in the application (memory requirements limited to the size
> of buffer choosen when creating the BufferedStream).
>

This would definitely be a much more memory efficient way, if it's
possible to avoid the XmlDocument in the first place.

However, it would be difficult to determine before writing the node if
it would make the file too big. You would probably just have to
determine the possible maximum size of a node, and move on to the next
file when a node possibly could make the file too large instead of when
it actually would be making the file too large.

--
Göran Andersson
_____
http://www.guffa.com
Author
17 Dec 2008 11:02 PM
Anthony Jones
Show quote Hide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:uH3dCi%23XJHA.5276@TK2MSFTNGP03.phx.gbl...
> Anthony Jones wrote:
>>
>> "Stanimir Stoyanov" <stoya***@REMOVETHIS.live.com> wrote in message
>> news:7F78CCB2-E9E0-4FB2-BE1E-8C30E24DCFC7@microsoft.com...
>>> The length of the OuterXml string will not necessarily reflect the
>>> length of the data *on disk* depending on the encoding used when saving.
>>>
>>> I think it would be a better idea to save the xml document to a
>>> MemoryStream and then check its length, as proposed in my first reply to
>>> the OP.
>>
>> A far better approach would be to ditch XmlDocument altogether.
>>
>> Create XmlTextWriter on a BufferedStream on a FileStream.
>>
>> Use XmlTextWriter to create the Xml as needed which will just stream out
>> the destination file, the BufferedStream Length property would tell you
>> exacly how much has been written to the file.
>>
>> When appropriate the existing content can be closed off and a new pair of
>> streams and a writer could be created to continue with the next traunch
>> of content and so on.
>>
>> This approach allows you to accurately monitor the size of XML file being
>> created, doesn't require frequent large strings to be generated and
>> dropped (as using OuterXml.Length would do) and limits the demand for
>> memory in the application (memory requirements limited to the size of
>> buffer choosen when creating the BufferedStream).
>>
>
> This would definitely be a much more memory efficient way, if it's
> possible to avoid the XmlDocument in the first place.
>
> However, it would be difficult to determine before writing the node if it
> would make the file too big. You would probably just have to determine the
> possible maximum size of a node, and move on to the next file when a node
> possibly could make the file too large instead of when it actually would
> be making the file too large.


Yes I think you would need some such heuristics on it and you may be cutting
a file shorter than it needed to be as a result.

--
Anthony Jones - MVP ASP/ASP.NET
Author
16 Dec 2008 4:42 PM
Göran_Andersson
Stanimir Stoyanov wrote:
> The length of the OuterXml string will not necessarily reflect the
> length of the data *on disk* depending on the encoding used when saving.

Yes, I ment to mention that, but I forgot. :)

It may also differ depending on how the XML code is indented.

> I think it would be a better idea to save the xml document to a
> MemoryStream and then check its length, as proposed in my first reply to
> the OP.

That way you will only know if the documnet is too large or not, it will
not tell you how many nodes you can put in each file.

I would get the XML code for each node, encode into a byte array and
write to the file. That way you know exactly how much is written, and
you can finish up the file when it's full and continue writing to a new
file.

That means of course that you have to write the XML header and root
element to the file yourself.

--
Göran Andersson
_____
http://www.guffa.com
Author
15 Dec 2008 9:16 PM
Stanimir Stoyanov
P.S. > Is there a way to get size of an object stored in memory?

There is no single answer to this part of your question. You have to
consider value and reference types.

The amount of memory occupied by value types such as integral values or
structures can be easily predicted because their size is fixed. You can do
so by calling System.Runtime.InteropServices.Marshal.SizeOf against the type
or the particular value.

Regarding reference types, there is no unified way to get an object's size.
For example, an the representation of an XmlDocument in memory is not
textual (as it is written on disk) but a tree of nodes, each containing its
relevant data. Only after the data is "flattened", can you 'observe' its
size. Encodings take a significant part here too--a character in ASCII is 1
byte long while Unicode uses two or even more bytes to represent a single
character.

--
Stanimir Stoyanov
http://stoyanoff.info

Show quoteHide quote
"ApeX" <mmo***@gmail.com> wrote in message
news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...
> 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?
>
> Thanx!
Author
15 Dec 2008 9:18 PM
Jeff Johnson
"ApeX" <mmo***@gmail.com> wrote in message
news:7170a3f1-7f01-4998-b045-4e7c1a899482@u18g2000pro.googlegroups.com...

> 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?

I take it XmlDocument.InnerXml.Length is not what you're looking for?
Author
15 Dec 2008 11:36 PM
Göran_Andersson
ApeX wrote:
> 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...

--
Göran Andersson
_____
http://www.guffa.com

Bookmark and Share