XmlSerializer(Type type, Type[] extraTypes) 内存泄漏
在使用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) 内存泄漏的更多相关文章
- 巧妙设置Texture Type,将ShadowMask内存占用变成之前的1/4
0x00 前言 在这篇文章中,我选择了过去一周Unity官方社区交流群中比较有代表性的几个问题,总结在这里和大家进行分享.同时,也欢迎大家加入我们这个讨论干货的官方技术群,交流看法分享经验. Unit ...
- MSDN官方XmlSerializer类导致内存泄漏和性能低
MSDN官方XmlSerializer类使用说明链接: http://msdn.microsoft.com/zh-CN/library/system.xml.serialization.xmlseri ...
- 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 ...
- const type& 与 type& 的区别
const type& 与 type& 是C/C++编程中容易混淆的两个知识点,现在以 cont int& 与 int& 为例讲解: 1.int& 讲解 int ...
- [Latex] 所有字体embedded: Type3 PDF文档处理 / True Type转换为Type 1
目录: [正文] Adobe Acrobat打印解决字体嵌入问题 [Appendix I] Type3转TRUE Type/Type 1 [Appendix II] TRUE Type转Type 1 ...
- 【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_ ...
- /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 ...
- 转载:oracle 自定义类型 type / create type
标签:type create oracle object record 一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarc ...
- oracle 自定义类型 type / create type
一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...
随机推荐
- LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2
https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...
- java格式化时间格式
System.out.println("Hello World!"); SimpleDateFormat format = new SimpleDateFormat( " ...
- Maven 排除第三方jar包所依赖的其他依赖
单依赖过滤:可以过滤一个或者多个,如果过滤多个要写多个<exclusion>. <dependency> <groupId>org.apache.hbase< ...
- Maven 从svn下载后,pom.xml报错解决方案
Multiple annotations found at this line: - Execution default-testResources of goal org.apache.maven. ...
- Java接口中的方法
接口中可以含有变量和方法.但是,接口中的变量会被隐式地指定为public static final变量(并且只能是public static final变量,用private修饰会报编译错误),而方法 ...
- jvm 之结构与机制
本文旨在给所有希望了解JVM(Java Virtual Machine)的同学一个概念性的入门,主要介绍了JVM的组成部分以及它们内部工作的机制和原理.当然本文只是一个简单的入门,不会涉及过多繁杂的参 ...
- 作业七:团队项目——Alpha版本冲刺阶段008
今日进度:组内成员讨论 今日安排:组内成员分工
- linux中的内存申请函数的区别 kmalloc, vmalloc
kmalloc是返回连续内存的内存分配函数 vmalloc是返回较大内存空间的,不需要连续的内存分配函数.其速度较慢,并且不能在中断上下文调用.
- windows、ubuntu下eclipse搭建java、Python环境问题总结
前两篇博文分别讲述了如何在windows.ubuntu下用eclipse搭建java.python环境,下面就针对本人遇到的问题做一个总结. 一.windows下关于java环境变量JAVA_HOME ...
- C语言实现粒子群算法(PSO)一
最近在温习C语言,看的书是<C primer Plus>,忽然想起来以前在参加数学建模的时候,用过的一些智能算法,比如遗传算法.粒子群算法.蚁群算法等等.当时是使用MATLAB来实现的,而 ...