在使用XmlSerializer进行序列化或者反序列的时候,对于下面的两个构造方法

XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)

为了提高性能,XML 序列化基础结构将动态生成程序集,以序列化和反序列化指定类型。此基础结构将查找并重复使用这些程序集。

但是对于XmlSerializer(Type type, Type[] extraTypes) 等其他的构造函数,则会生成同一程序集的多个版本,且绝不会被卸载,这将导致内存泄漏和性能降低。

为了解决这个问题代码如下,仅供参考:

static Hashtable ht = new Hashtable();

        /// <summary>
/// 将指定的对象序列化为XML格式的字符串并返回。
/// </summary>
/// <param name="o">待序列化的对象</param>
/// <returns>返回序列化后的字符串</returns>
public static string Serialize(Object o, Type[] types = null)
{
if (o != null)
{ string xml = "";
XmlSerializer serializer = null;
try
{ if (types != null)
{
string names = "";
for (int i = 0; i < types.Length; i++)
{
names = names + types[i].FullName;
}
XmlSerializerFactory factory = new XmlSerializerFactory();
if (ht.ContainsKey(names))
{
serializer = (XmlSerializer)ht[names];
}
else
{
serializer = factory.CreateSerializer(o.GetType(), types);
ht.Add(names, serializer);
} // serializer = new XmlSerializer(o.GetType(), types);
}
else
{
serializer = new XmlSerializer(o.GetType());
} using (MemoryStream mem = new MemoryStream())
{
using (XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
XmlSerializerNamespaces n = new XmlSerializerNamespaces();
n.Add("", "");
serializer.Serialize(writer, o, n); mem.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(mem))
{
xml = reader.ReadToEnd();
}
}
}
types = null;
serializer = null;
}
catch { xml = ""; serializer = null; }
return xml;
}
else
{
return "";
}
}

XmlSerializer(Type type, Type[] extraTypes) 内存泄漏的更多相关文章

  1. 巧妙设置Texture Type,将ShadowMask内存占用变成之前的1/4

    0x00 前言 在这篇文章中,我选择了过去一周Unity官方社区交流群中比较有代表性的几个问题,总结在这里和大家进行分享.同时,也欢迎大家加入我们这个讨论干货的官方技术群,交流看法分享经验. Unit ...

  2. MSDN官方XmlSerializer类导致内存泄漏和性能低

    MSDN官方XmlSerializer类使用说明链接: http://msdn.microsoft.com/zh-CN/library/system.xml.serialization.xmlseri ...

  3. Unable to create a constant value of type 'Closure type'

    使用Linq to Entities的时候发生如下异常: Unable to create a constant value of type 'Closure type'. Only primitiv ...

  4. const type& 与 type& 的区别

    const type& 与 type& 是C/C++编程中容易混淆的两个知识点,现在以 cont int& 与 int& 为例讲解: 1.int& 讲解 int ...

  5. [Latex] 所有字体embedded: Type3 PDF文档处理 / True Type转换为Type 1

    目录: [正文] Adobe Acrobat打印解决字体嵌入问题 [Appendix I] Type3转TRUE Type/Type 1 [Appendix II] TRUE Type转Type 1 ...

  6. 【error】no type named ‘type’ in ‘class std::result_of<void

    Q: std::thread fs_module(fs_process, prob_orig, fb_sz, line_num, probp, plabel, std::ref(confidence_ ...

  7. /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void

    /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std ...

  8. 转载:oracle 自定义类型 type / create type

    标签:type create oracle object record 一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarc ...

  9. oracle 自定义类型 type / create type

    一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...

随机推荐

  1. php的clone 浅拷贝

    总所周知 php 的 clone方法 拷贝一个对象 而且还是所谓的浅拷贝 一时迷茫 今天终于整明白了 <?php class a { pulic $data; function __constr ...

  2. windows8.1下javaweb环境搭建及基本配置(jdk+tomcat+eclipse)

    1.下载安装jdk在无空格的路径下,否则在linux下可能出问题.配置环境变量: a.新建系统变量——JAVA_HOME,值——D:\programming\java\jdk8 // win8下若建为 ...

  3. Mysql Concat()bug

    1.http://bugs.mysql.com/bug.php?id=12030 CONCAT with INTEGER modifies result-set encoding 2.这个bug的修改 ...

  4. 利用反射模拟一个spring的内部工作原理

    这个简单的案例是实行了登录和注册的功能,没有链接数据库.  在bean中id 是唯一的,id和name的区别在于id不能用特殊字符而name可以用特殊字符,比如:/-\.... package com ...

  5. android-webview开发中的各种使用方法(持续更,尽量全)

    最新坑A:(没看过的可以从下面开始处看起): 测试部门测出来一个坑,当多次点击退出后,会出现app崩溃现象,报如下错误: java.lang.IllegalArgumentException: Rec ...

  6. React之事件绑定、列表中key的使用

    在学习React的Hadding Events这一章节,发现事件回调函数的几种写法,看似区别不大,但实际差异还是蛮大的. class Toggle extends React.Component{ c ...

  7. Access restriction: The type TaskTopicResolver is not accessible due to restrict

    Access restriction: The type TaskTopicResolver is not accessible due to restrict :  Eclipse 默认把这些受访问 ...

  8. Altium Designer自动更新——解决方法

    今天,打开AD,一直显示更新,关机重启也不管事. 然后,我把AD安装目录下,system文件夹下的Installation文件夹删了.就不再更新了.

  9. 例子:RSS Reader Sample

    本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析. SyndicationFeed 解析完成后 可以得到SyndicationItem ...

  10. firefox,跨域ajax 调用方法

    在A站点ajax 调用B站的页面(方法)时, 使用post,且dataType类型为jsonp 有时在IE会有No Transport的错误提示,请加 jQuery.support.cors = tr ...