SerializeObject抛出System.OutOfMemoryException [英] SerializeObject throws System.OutOfMemoryException

查看:692
本文介绍了SerializeObject抛出System.OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"JsonConvert.SerializeObject"存在严重问题,我需要序列化超过500,000个字典记录以使序列化引发以下错误; System.OutOfMemoryException.我试图在foreach中分别序列化每个键,值对,但是它被锁定了.显然这是一个优化问题,但是我不知道从哪里开始,要序列化部分的线程? 这些功能可以正常工作,几乎没有值. 我的代码:

I have a serious problem with "JsonConvert.SerializeObject" I need to serialize more than 500,000 dictionary records to make serialize throws the following error; System.OutOfMemoryException. I tried to serialize each key, value pair individually in a foreach but it's locked. Apparently it is an optimization problem, but I do not know where to start, threads to serialize in parts? These functions works fine with few values. My code:

string json = JsonConvert.SerializeObject(DatatableToDictionary(dt), Newtonsoft.Json.Formatting.Indented);

public List<Dictionary<string, object>> DatatableToDictionary(DataTable dt, List<DataColumn> columns)
{
    return dt.Rows.Cast<DataRow>().Select(
         r => columns.ToDictionary(c => c.ColumnName, c => r[c.ColumnName])).ToList();
}

推荐答案

在处理大量数据时,可以将其流式传输到文件中,以免一次全部存储在内存中.

When you're dealing with a large amount of data, you can stream it to a file to avoid having it all in memory at once.

var filePath = @"C:\somewhere.json";

using (var fs = File.Open(filePath, FileMode.CreateNew))
using (var sw = new StreamWriter(fs))
using (var jw = new JsonTextWriter(sw))
{
    var serializer = new JsonSerializer();
    serializer.Serialize(jw, dictionary);
}

这将一次序列化一点,并避免在内存中有一个巨大的字符串.

This will serialize a bit at a time and avoid having a giant string in memory.

这篇关于SerializeObject抛出System.OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆