SerializeObject throws System.OutOfMemoryException

ぃ、小莉子 提交于 2019-12-12 11:26:16

问题


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();
}

回答1:


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.



来源:https://stackoverflow.com/questions/39598171/serializeobject-throws-system-outofmemoryexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!