Memory usage is high when you create several XmlSerializer objects in ASP.NET

Article translations Article translations
Article ID: 886385 - View products that this article applies to.
Expand all | Collapse all

SYMPTOMS

In Microsoft ASP.NET, the memory usage increases unexpectedly when you create several XmlSerializer objects.

CAUSE

This problem occurs because an assembly that contains Microsoft intermediate language (MSIL) is created and loaded into memory when you create an XmlSerializer object. You cannot unload the assembly without unloading the application domain that it resides in. Therefore, when you create several XmlSerializer objects, you may notice unexpectedly high memory usage.

For example, if you use the following constructor to create several XmlSerializer objects, a new dynamic assembly is created every time:
public void XmlSerializer( Type t, Type[] extraTypes)

RESOLUTION

To work around this problem of re-creating dynamic assemblies, use one of the following methods:
  • Create one instance of the XmlSerializer class, and put that instance in the cache by using the caching APIs. For example, for a .dll file that is named HighSchool, the following code caches one instance of the XmlSerializer class:
    XmlSerializer mySerializer = new XmlSerializer(typeof(HighSchool.MyClass), attrOverrides, extraTypes, root, "http://www.microsoft.com");
    Cache["HighSchoolSerializer"] = mySerializer 
    
    Use the instance of the XmlSerializer class that you put in the cache instead of creating a new XmlSerializer object every time.
  • Use the following XmlSerializer class constructors. These class constructors cache the assemblies:
    In the .NET Framework version 1.0
     public XmlSerializer(Type);

    In the .NET Framework version 1.1
    public XmlSerializer(Type type);
    public XmlSerializer(Type type, string defaultNamespace); 
  • Declare the XmlSerializer object to be a static member of a class.

REFERENCES

For additional information about the XmlSerializer class and about the XmlSerializer class constructors, visit the following Microsoft Developer Network (MSDN) Web sites:
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(vs.71).aspx
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer_members(vs.71).aspx

Properties

Article ID: 886385 - Last Review: February 24, 2012 - Revision: 4.0
APPLIES TO
  • Microsoft .NET Framework 4
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
Keywords: 
kbtshoot kbprb KB886385

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com