<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: .NET and C# Garbage Collection</title>
	<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/</link>
	<description>Quality ASP.NET/C#/SQL Server tutorials, examples, videos and advice</description>
	<pubDate>Sat, 19 May 2012 16:04:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>

	<item>
		<title>By: Aramis1986</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-239</link>
		<dc:creator>Aramis1986</dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:05:58 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-239</guid>
		<description>I dont know why it cuts my code.</description>
		<content:encoded><![CDATA[<p>I dont know why it cuts my code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aramis1986</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-238</link>
		<dc:creator>Aramis1986</dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:04:44 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-238</guid>
		<description>Ex. 1

for (int i =0; i </description>
		<content:encoded><![CDATA[<p>Ex. 1</p>
<p>for (int i =0; i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aramis1986</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-237</link>
		<dc:creator>Aramis1986</dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:02:51 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-237</guid>
		<description>What is better for performance and how it should be done, creating variable in loop or before loop as in examples :

Ex. 1
            for (int i = 0; i </description>
		<content:encoded><![CDATA[<p>What is better for performance and how it should be done, creating variable in loop or before loop as in examples :</p>
<p>Ex. 1<br />
            for (int i = 0; i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Bevitt</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-198</link>
		<dc:creator>Charles Bevitt</dc:creator>
		<pubDate>Mon, 21 Sep 2009 19:51:14 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-198</guid>
		<description>Hmmmm .... The ins and outs of garbage collection and memory management is one of those things I've always meant to get around to studying and never have (I word in a small IT shop and therefore do it all - my knowledge is broad but not deep).

But - just call Close() and Dispose() as soon as you can on EVERY .Net object that offeres these methods, and sleep easy at night.</description>
		<content:encoded><![CDATA[<p>Hmmmm &#8230;. The ins and outs of garbage collection and memory management is one of those things I&#8217;ve always meant to get around to studying and never have (I word in a small IT shop and therefore do it all - my knowledge is broad but not deep).</p>
<p>But - just call Close() and Dispose() as soon as you can on EVERY .Net object that offeres these methods, and sleep easy at night.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-172</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Mon, 24 Aug 2009 19:49:55 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-172</guid>
		<description>Ted, thanks for the feedback. I have a few answers to your questions...

&lt;blockquote&gt;1) Are you creating 10,000 objects for every user that hits your web application?&lt;/blockquote&gt;

No, this is an admin application where a few users are occasionally performing work on ~10000 records. For example, generating PDF documents for records, or exporting to a CSV file, or performing some sort of mass update.

&lt;blockquote&gt;2) How are you storing the data in the MyObject class? What kind of data types are you
using?&lt;/blockquote&gt;

Actually, the MyObject class is typically a class representing an entity. For example, a Student record let's say. The student record is for the most part pretty basic, with simple short string properties. But, it does have a IDictionary property that has approximately 300 items. This is a custom field -&#62; value dict that allows us to store arbitrarily defined data for the student. That is the big item for these objects. And, because this is actually stored as XML in the DB, I deserialize it into the dictionary when it is loaded from the DB. I perform this deserialization using an XmlReader and Serialize using XmlWriter. Certainly, this could be causing an issue, but I thought using the stream approach would be better than loading an XML document and looping through it.

&lt;blockquote&gt;3) I noticed you are using the StringBuilder class.&lt;/blockquote&gt;

Yes, and I also think this might be an issue. In the example I presented, I am basically storing a CSV record for each record in the string builder, then after the loop completes, I create and write the contents to a CSV file. I have already started modifying this approach to stream to the file during the processing, rather than creating the x-large SB object.

I have also seen some others discuss issues where you do not set an initial size for the SB, and the SB has to dynamically allocate more memory.

Thanks!

Kevin</description>
		<content:encoded><![CDATA[<p>Ted, thanks for the feedback. I have a few answers to your questions&#8230;</p>
<blockquote><p>1) Are you creating 10,000 objects for every user that hits your web application?</p></blockquote>
<p>No, this is an admin application where a few users are occasionally performing work on ~10000 records. For example, generating PDF documents for records, or exporting to a CSV file, or performing some sort of mass update.</p>
<blockquote><p>2) How are you storing the data in the MyObject class? What kind of data types are you<br />
using?</p></blockquote>
<p>Actually, the MyObject class is typically a class representing an entity. For example, a Student record let&#8217;s say. The student record is for the most part pretty basic, with simple short string properties. But, it does have a IDictionary property that has approximately 300 items. This is a custom field -&gt; value dict that allows us to store arbitrarily defined data for the student. That is the big item for these objects. And, because this is actually stored as XML in the DB, I deserialize it into the dictionary when it is loaded from the DB. I perform this deserialization using an XmlReader and Serialize using XmlWriter. Certainly, this could be causing an issue, but I thought using the stream approach would be better than loading an XML document and looping through it.</p>
<blockquote><p>3) I noticed you are using the StringBuilder class.</p></blockquote>
<p>Yes, and I also think this might be an issue. In the example I presented, I am basically storing a CSV record for each record in the string builder, then after the loop completes, I create and write the contents to a CSV file. I have already started modifying this approach to stream to the file during the processing, rather than creating the x-large SB object.</p>
<p>I have also seen some others discuss issues where you do not set an initial size for the SB, and the SB has to dynamically allocate more memory.</p>
<p>Thanks!</p>
<p>Kevin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ted</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-171</link>
		<dc:creator>ted</dc:creator>
		<pubDate>Sat, 22 Aug 2009 19:42:58 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-171</guid>
		<description>Hi Kevin.  Thanks for posting your question.

While it is true that the objects you are instantiating in the loop will go out of scope 
when the loop completes, that does NOT mean that the memory allocated and used for those 
objects will be released back to the ASP.NET worker process at that time.  That release 
of the memory will only occur during the next Garbage Collection execution.  So rather 
than trying to cleanup the memory, which you cannot control, you have to examine your 
algorithm and data types to find the best solution to resolve the memory problem.  In 
other words, you have to change the way that you designed the code.  That is how you can 
solve this memory usage problem.

There could be many reasons that you are getting the Out of Memory error.  Also, it could 
be a combination of of things.  Here are some things to consider.

1) Are you creating 10,000 objects for every user that hits your web application?  That 
appears to be a lot of memory usage if the size of MyObject is large as you say below in 
your comment.  The way you estimate how much available ASP.NET RAM you need for that 
block of code to run successfully is by using this equation: (Est. size of MyObject) * (# 
of objects created in your loop) * (Max # of concurrent users).  For example, if the 
estimated size of MyObject is 10K bytes, you are creating 10,000 MyObjects in the loop 
and your application has max 50 concurrent users, you would need about 10k * 10,000 * 50 
= 5000000K bytes of memory available.  If your object is large, it is probably much more 
than 10k in my example.

Is there a need to loop and create 10,000 objects for every user that hits the 
application?  Why do you need so many?  Can you reduce the loop iteration count to much 
less?  This of course requires analysis of your overall algorithm and maybe even business 
requirements.

2) How are you storing the data in the MyObject class?  What kind of data types are you 
using?  Are those the most efficient data types with regards to memory?  You can e-mail 
me the code for that class and I can look at it if you want.

3) I noticed you are using the StringBuilder class.  Although StringBuilder is generally 
much better for performance versus using String concatenation, you can try this to see if 
less memory is consumed.  Try to give the StringBuilder a size when you instantiate it.  
How big do you think the string will be once you have appended everything to it?  If you 
think the average largest size of the string is going to be 5Megabytes, then you can 
instantiate the StringBuilder like this:
StringBuilder mybuilder = new StringBuilder(5242880);

This tells ASP.NET to allocate 5Meg worth of space right away and it will not require any 
further allocation whenever you append data to string using the Append method.  What this 
does is ensure that you are not allocating any new blocks of memory each time in the loop 
with regards to the StringBuilder-this does not affect the memory needed for the MyObject 
class instantiations.

Yours,
Ted Kolovos</description>
		<content:encoded><![CDATA[<p>Hi Kevin.  Thanks for posting your question.</p>
<p>While it is true that the objects you are instantiating in the loop will go out of scope<br />
when the loop completes, that does NOT mean that the memory allocated and used for those<br />
objects will be released back to the ASP.NET worker process at that time.  That release<br />
of the memory will only occur during the next Garbage Collection execution.  So rather<br />
than trying to cleanup the memory, which you cannot control, you have to examine your<br />
algorithm and data types to find the best solution to resolve the memory problem.  In<br />
other words, you have to change the way that you designed the code.  That is how you can<br />
solve this memory usage problem.</p>
<p>There could be many reasons that you are getting the Out of Memory error.  Also, it could<br />
be a combination of of things.  Here are some things to consider.</p>
<p>1) Are you creating 10,000 objects for every user that hits your web application?  That<br />
appears to be a lot of memory usage if the size of MyObject is large as you say below in<br />
your comment.  The way you estimate how much available ASP.NET RAM you need for that<br />
block of code to run successfully is by using this equation: (Est. size of MyObject) * (#<br />
of objects created in your loop) * (Max # of concurrent users).  For example, if the<br />
estimated size of MyObject is 10K bytes, you are creating 10,000 MyObjects in the loop<br />
and your application has max 50 concurrent users, you would need about 10k * 10,000 * 50<br />
= 5000000K bytes of memory available.  If your object is large, it is probably much more<br />
than 10k in my example.</p>
<p>Is there a need to loop and create 10,000 objects for every user that hits the<br />
application?  Why do you need so many?  Can you reduce the loop iteration count to much<br />
less?  This of course requires analysis of your overall algorithm and maybe even business<br />
requirements.</p>
<p>2) How are you storing the data in the MyObject class?  What kind of data types are you<br />
using?  Are those the most efficient data types with regards to memory?  You can e-mail<br />
me the code for that class and I can look at it if you want.</p>
<p>3) I noticed you are using the StringBuilder class.  Although StringBuilder is generally<br />
much better for performance versus using String concatenation, you can try this to see if<br />
less memory is consumed.  Try to give the StringBuilder a size when you instantiate it.<br />
How big do you think the string will be once you have appended everything to it?  If you<br />
think the average largest size of the string is going to be 5Megabytes, then you can<br />
instantiate the StringBuilder like this:<br />
StringBuilder mybuilder = new StringBuilder(5242880);</p>
<p>This tells ASP.NET to allocate 5Meg worth of space right away and it will not require any<br />
further allocation whenever you append data to string using the Append method.  What this<br />
does is ensure that you are not allocating any new blocks of memory each time in the loop<br />
with regards to the StringBuilder-this does not affect the memory needed for the MyObject<br />
class instantiations.</p>
<p>Yours,<br />
Ted Kolovos</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-170</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Sat, 22 Aug 2009 17:20:56 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-170</guid>
		<description>I am having a problem that I think is related to these concepts, but I am unsure of how to handle or correct the situation. I have something like the following...

StringBuilds output = new StringBuilder();
IList myIDs = GetMyIDs(); // Returns ~10,000 ids
foreach (int id in myIDs)
{
  MyObject myObj = GetMyObject(id); // Returns a new object. Very large object (holds alot of XML data in a property for example.)
output.Append(myObj.Render()); // Just appends some text to the SB.
}

I am getting System.OutOfMemory exceptions.

Based on your tutorial, each of the 10,000 objects instantiated will hold a place in memory until they go out of scope. I think that this means the scope only ends after the loop completes processing. So, is there a way to release these resources earlier than the end of the loop?

Thanks!

Kevin</description>
		<content:encoded><![CDATA[<p>I am having a problem that I think is related to these concepts, but I am unsure of how to handle or correct the situation. I have something like the following&#8230;</p>
<p>StringBuilds output = new StringBuilder();<br />
IList myIDs = GetMyIDs(); // Returns ~10,000 ids<br />
foreach (int id in myIDs)<br />
{<br />
  MyObject myObj = GetMyObject(id); // Returns a new object. Very large object (holds alot of XML data in a property for example.)<br />
output.Append(myObj.Render()); // Just appends some text to the SB.<br />
}</p>
<p>I am getting System.OutOfMemory exceptions.</p>
<p>Based on your tutorial, each of the 10,000 objects instantiated will hold a place in memory until they go out of scope. I think that this means the scope only ends after the loop completes processing. So, is there a way to release these resources earlier than the end of the loop?</p>
<p>Thanks!</p>
<p>Kevin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sif</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-166</link>
		<dc:creator>Sif</dc:creator>
		<pubDate>Wed, 12 Aug 2009 22:41:17 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-166</guid>
		<description>awesome... you are one hell of a teacher, keep up the good work.  thanks a lot for all your help, and to think you're doin it for free... why not setup a donation link and well wishers like myself could chip a bit to compensate your time and effort spent in creating this very very very wonderfull and educational tutorial))</description>
		<content:encoded><![CDATA[<p>awesome&#8230; you are one hell of a teacher, keep up the good work.  thanks a lot for all your help, and to think you&#8217;re doin it for free&#8230; why not setup a donation link and well wishers like myself could chip a bit to compensate your time and effort spent in creating this very very very wonderfull and educational tutorial))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vidya</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-120</link>
		<dc:creator>Vidya</dc:creator>
		<pubDate>Wed, 27 May 2009 06:16:35 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-120</guid>
		<description>An awesome article. Thank you very much for the information you have provided. Please keep writing such articles.</description>
		<content:encoded><![CDATA[<p>An awesome article. Thank you very much for the information you have provided. Please keep writing such articles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ola</title>
		<link>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-111</link>
		<dc:creator>ola</dc:creator>
		<pubDate>Wed, 20 May 2009 02:46:18 +0000</pubDate>
		<guid>http://www.csharpuniversity.com/2009/02/18/net-and-c-garbage-collection/#comment-111</guid>
		<description>I really appreciate this tutorial,infact its an eye opener for me and I commend your efforts for putting all this stuff together in a succint manner, now a more confident with my C# skill.
Keep the good work.</description>
		<content:encoded><![CDATA[<p>I really appreciate this tutorial,infact its an eye opener for me and I commend your efforts for putting all this stuff together in a succint manner, now a more confident with my C# skill.<br />
Keep the good work.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

