在使用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. 在APACHE服务器上的访问方式上去除index.php

      在APACHE服务器上的访问方式上去除index.php   下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index ...

  2. [css]水平垂直居中的方法

    1.top:cale(50% - 2rem); left:cale(50% - 2rem);

  3. LeetCode 177 Nth-Highest Salary mysql,取第n条数据,limit子句 难度:1

    https://leetcode.com/problems/nth-highest-salary/ ATTENTION:limit 子句只能接受int常量,不能接受运算式 CREATE FUNCTIO ...

  4. jsp页面往mysql里插入中文后数据库里显示乱码

    1.JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要在页面开始地方用下面代码指定字符集编码即可,<%@ page contentType="text ...

  5. MySQL主从同步报错排错结果及修复过程之:Slave_SQL_Running: No

    起因调查: 收到大量邮件报警想必事出有因,就问同事到底发生了什么?同事登录从库查看,发现出现如下报错提示,表示与主库同步失败,一直卡在哪里,看他弄了两个多小时,问题越来越多,解决一个恢复平静了一两分钟 ...

  6. R语言入门系列1--数学狗还是做数据好了

    nanana,作为一个不合格的数学专业学生,脑袋不好使,又穷逼,只好学点儿实用的东西,希望能养活自己~~~ 不瞎哔哔,想做数据方面工作的时候在犹豫是学R还是学python,一点儿python基础都没有 ...

  7. linux配置网卡绑定

    1.确定好要绑定的那两个网口 我这边要绑定的是两个业务口  em2.em3 2.配置ifcfg-bond0.em2.em3 3.修改配置文件/etc/modprcode.d/disk.conf 底部添 ...

  8. DateTime 详解

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  9. C#编程:SqlCommand.Parameters.Add()方法的参数问题。

    在存储过程中添加2个参数 sql语句 例: “update [tablename] username = @username where id=@id” 然后把需要的 command.Paramete ...

  10. 【zz】matlab 腐蚀膨胀算法

    1.图像膨胀的Matlab实现: 可以使用imdilate函数进行图像膨胀,imdilate函数需要两个基本输入参数,即待处理的输入图像和结构元素对象.结构元素对象可以是strel函数返回的对象,也可 ...