<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://xmladvice.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx</link><description>Paul Wilson emailed me with some questions regarding perceived memory leaks related to System.Xml.Serialization.XmlSerializer. I questioned whether it would be a &amp;#8220;memory leak&amp;#8221;, but pinged Christoph Schittko for some backup evidence. He said</description><dc:language>en</dc:language><generator>CommunityServer 2.1 (Build: 60809.935)</generator><item><title>re: Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#5976</link><pubDate>Thu, 12 Feb 2004 01:17:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:5976</guid><dc:creator>kaevans</dc:creator><description>I would still call this a bug personally.  Several of the constructor overloads do reuse the dynamically created assembly, and there's no intrinsic reason the others could not do so as well.  Maybe someone ran out of time, or patience, in creating the appropriate caching algorithms, and just decided these other cases weren't important enough.  But anytime you run a loop over a few identical statements and &amp;quot;leak&amp;quot; more memory every time -- that's a bug!  And yes, while the leak is actually a dynamically created assembly, the net result is that it still profoundly affects memory, so it is also a memory leak.&lt;br&gt;&lt;br&gt;By the way, we had a rather interesting twist to this leak.  We are using XmlSerialization to save user actions as reusable tasks.  We also track their history to allow undos, and we save a portion of their history to allow them to get back to the same setup, kind of like a macro.  OK, so lets say they performed a good number of tasks, each of which was serialized as part of their history.  Each one of these tasks ends up being associated with a memory leak, but of course eventually they close the app and get the memory back.  Oops!  The gotcha for us was that the next time they can start from where they last left off, by having us reapply all of the tasks, which requires all the deserializations.  The net result was that we instantly had a situation where we were actually recreating the very memory leaks from the last session!</description></item><item><title>re: Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#5977</link><pubDate>Thu, 12 Feb 2004 03:36:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:5977</guid><dc:creator>kaevans</dc:creator><description>Paul,&lt;br&gt;&lt;br&gt;Are you sure the memory really leaks, i.e. it's NEVER claimed by the garbage collection? Have you observed that the GC ran for all generations while you monitored memory usage? The fact that some assemblies are not cached doesn't mean they are leaked.&lt;br&gt;&lt;br&gt;What exactly are you observing to determine that memory is leaking? &lt;br&gt;&lt;br&gt;Also, the article Kirk quoted states that only some of more complex constructors of the XmlSerializer cause the XmlSerializer to not cache the temp assemblies. Is there a way to switch to simpler constructs?&lt;br&gt;&lt;br&gt;Christoph</description></item><item><title>re: Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#5979</link><pubDate>Thu, 12 Feb 2004 13:53:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:5979</guid><dc:creator>kaevans</dc:creator><description>Yes its a memory leak -- its NEVER reclaimed by the GC.  Its the actual dynamic assembly that is &amp;quot;lost&amp;quot;, which isn't managed memory.  Some of the .net memory counters do increase a little, but its the private bytes of the process that go through the roof.  Run this code snippet as the only file in a console app with performance monitor.  With the leakMemory variable set to true, then run it again with leakMemory set to false.  The only difference is whether or not the XmlSerializer is created each time, or only once.  My last test had a fatal memory crash after leaking almost 500MB, which had long since pushed me far into the page file.  All memory was reclaimed when the process was killed, or ended if you make it that far, but the GC never manages it or reclaims it.  Can we switch to a simpler constructor?  No.  We could possibly use a different one with more work, but the other links I provided on my blog indicate that most of the constructors, except the very simplest, leak dynamic assemblies. &lt;br&gt;&lt;br&gt;using System;&lt;br&gt;using System.IO;&lt;br&gt;using System.Text;&lt;br&gt;using System.Xml.Serialization;&lt;br&gt;&lt;br&gt;namespace XmlLeak&lt;br&gt;{&lt;br&gt;	class Global&lt;br&gt;	{&lt;br&gt;		private static bool leakMemory = true;&lt;br&gt;		private static XmlSerializer serial = null;&lt;br&gt;&lt;br&gt;		[STAThread]&lt;br&gt;		private static void Main(string[] args) {&lt;br&gt;			for (int index = 0; index &amp;lt; 1000000; index++) {&lt;br&gt;				Test test = new Test();&lt;br&gt;				test.Id = index;&lt;br&gt;				test.Time = DateTime.Now;&lt;br&gt;				StringBuilder builder = new StringBuilder();&lt;br&gt;				StringWriter writer = new StringWriter(builder);&lt;br&gt;				if (leakMemory || serial == null) {&lt;br&gt;					serial = new XmlSerializer(typeof(Base), new Type[] {typeof(Test)});&lt;br&gt;				}&lt;br&gt;				serial.Serialize(writer, test);&lt;br&gt;				string xml = builder.ToString();&lt;br&gt;			}&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	[Serializable()]&lt;br&gt;	public class Test : Base&lt;br&gt;	{&lt;br&gt;		private DateTime time;&lt;br&gt;		public DateTime Time {&lt;br&gt;			get { return time; }&lt;br&gt;			set { time = value; }&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	[Serializable()]&lt;br&gt;	public class Base&lt;br&gt;	{&lt;br&gt;		private int id;&lt;br&gt;		public int Id {&lt;br&gt;			get { return id; }&lt;br&gt;			set { id = value; }&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;</description></item><item><title>re: Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#6022</link><pubDate>Sun, 12 Sep 2004 23:02:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:6022</guid><dc:creator>kaevans</dc:creator><description>Any ways to get around the problem then?</description></item><item><title>re: Assembly Caching and XmlSerializer</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#6051</link><pubDate>Fri, 22 Jul 2005 19:30:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:6051</guid><dc:creator>kaevans</dc:creator><description>I share with you a workaround I found when serializing an ArrayList (when all the items are the same Type, that is &amp;quot;MyClass&amp;quot;)&lt;br&gt;&lt;br&gt;You'll see 3 examples:&lt;br&gt;1- Serializing the ArrayList &amp;quot;as is&amp;quot;&lt;br&gt;2- Caching the XmlSerlializer instance&lt;br&gt;3- Converting the ArrayList to a typed array &lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://www.singletonsoftware.com.ar/XmlSerializer/SerTest.cs"&gt;http://www.singletonsoftware.com.ar/XmlSerializer/SerTest.cs&lt;/a&gt;&lt;br&gt;&lt;br&gt;Hope you find this useful.&lt;br&gt;Regards&lt;br&gt;&lt;br&gt;Alejandro&lt;br&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>Leaks with the XmlSerilizer?</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#6087</link><pubDate>Thu, 12 Feb 2004 03:39:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:6087</guid><dc:creator>TrackBack</dc:creator><description /></item><item><title>Leaks with the XmlSerializer?</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#6088</link><pubDate>Thu, 12 Feb 2004 16:39:00 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:6088</guid><dc:creator>TrackBack</dc:creator><description /></item><item><title>XML Bugs \ Inconsistencies in .NET 2.0: The MS Support Call Process : Migration Woes Part 2</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#18060</link><pubDate>Wed, 24 May 2006 15:56:57 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:18060</guid><dc:creator>Dan's Archive</dc:creator><description /></item><item><title>XML Bugs \ Inconsistencies in .NET 2.0: The MS Support Call Process : Migration Woes Part 2</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#20314</link><pubDate>Fri, 04 Aug 2006 21:11:22 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:20314</guid><dc:creator>Dan's Archive</dc:creator><description /></item><item><title>ANSWER: POP QUIZ: What to do next? - part 1</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#40866</link><pubDate>Mon, 31 Mar 2008 17:56:26 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:40866</guid><dc:creator>ASP.NET Debugging</dc:creator><description>&lt;p&gt;So we didn't get much more on this one, so I'll go ahead and show how we find out what is going on here.&amp;amp;#160;&lt;/p&gt;
</description></item><item><title>Graco Nautilus 3 In 1 Car Seat</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#64781</link><pubDate>Fri, 07 May 2010 11:12:23 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:64781</guid><dc:creator>Graco Nautilus 3 In 1 Car Seat</dc:creator><description>&lt;p&gt;I appreciated reading through your blog as well as I've recomeneded to my readers here's the hyperlink &lt;a rel="nofollow" target="_new" href="http://www.graconautilus3in1carseats.com/tag/graco-nautilus-3-in-1-car-seat/"&gt;http://www.graconautilus3in1carseats.com/tag/graco-nautilus-3-in-1-car-seat/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>free blogs for teachers</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#64830</link><pubDate>Fri, 07 May 2010 19:08:05 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:64830</guid><dc:creator>free blogs for teachers</dc:creator><description>&lt;p&gt;We have been working on cranking out some new stuff for everyone. The latest is the Blogger Buddy gadget which allows you to quickly and easily view and post to your blogs on Blogger. com. It’ s still a little basic right now, but it gets the job done&lt;/p&gt;
</description></item><item><title>working from home jobs</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#65312</link><pubDate>Fri, 14 May 2010 11:23:20 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:65312</guid><dc:creator>working from home jobs</dc:creator><description>&lt;p&gt;Is credit card debt choking the life out of you? Using the credit card too often and incurring credit card debt seems to be today’ s disease. However, your predicament is partly the fault of credit card companies. They want to keep you in debt for as&lt;/p&gt;
</description></item><item><title>free blog hosting service</title><link>http://xmladvice.com/blogs/kaevans/archive/2004/02/11/5934.aspx#67310</link><pubDate>Wed, 02 Jun 2010 07:04:59 GMT</pubDate><guid isPermaLink="false">3e274673-4f98-48a3-a750-5e76696965bc:67310</guid><dc:creator>free blog hosting service</dc:creator><description>&lt;p&gt;Capture HD MP4 video and 5MP images and upload them directly to the Web1 with the Sony bloggie camera. The pocketable bloggie camera also features a unique swivel lens that rotates up to 270 degrees, making it easy to self- record a video clip. 1920 1080&lt;/p&gt;
</description></item></channel></rss>