添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
坏坏的眼镜  ·  【Unity】2D ...·  2 周前    · 
焦虑的春卷  ·  Comparison to ...·  7 月前    · 
踢足球的草稿本  ·  Regex in Python to ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a list with about 400 objects, and every time I try to serialize it I get an outofmemory exception.

The problem is that I am monitoring the server memory, and it never goes more than 40% usage, so I am kinda lost with this error.

str = JsonConvert.SerializeObject(list); catch(Exception ex) throw ex;

I double checked and the class serialized does not have complex type or reference to other object of the same type.

I even get the same exception if I try to do list.First()

namespace ilimitada.ServicioDistribucion.AnalisisDatos.Entidades
    using ilimitada.Dominio.Pervasive.SCI.Core.Enumeradores;
    using System;
    using System.Runtime.CompilerServices;
    public class CuentaCobrar
        public CuentaCobrar()
            this.Nit = string.Empty;
            this.TipoNit = string.Empty;
            this.RazonSocial = string.Empty;
            this.PrimerNombre = string.Empty;
            this.SegundoNombre = string.Empty;
            this.PrimerApellido = string.Empty;
            this.SegundoApellido = string.Empty;
            this.Direccion = string.Empty;
            this.CodigoCiudad = string.Empty;
            this.Indicativo = string.Empty;
            this.Telefono = string.Empty;
            this.Celular = string.Empty;
            this.Email = string.Empty;
            this.CodigoMunicipio = string.Empty;
            this.CodigoPais = string.Empty;
            this.Plazo = 0;
            this.CodigoActividadEconomica = string.Empty;
            this.Naturaleza = string.Empty;
            this.TieneRut = "No";
            this.Activo = "No";
            this.TipoTransaccion = Transaccion.Ninguna;
            this.Documento = string.Empty;
            this.OrdenFacturacion = string.Empty;
            this.DocumentoReferencia = string.Empty;
            this.SaldoDocumento = 0.0;
            this.FechaDocumento = DateTime.Now;
            this.FechaVencimiento = DateTime.Now;
            this.Compania = string.Empty;
        public string Activo { get; set; }
        public string Celular { get; set; }
        public string CodigoActividadEconomica { get; set; }
        public string CodigoCiudad { get; set; }
        public string CodigoMunicipio { get; set; }
        public string CodigoPais { get; set; }
        public string Direccion { get; set; }
        public string Documento { get; set; }
        public string DocumentoReferencia { get; set; }
        public string Email { get; set; }
        public DateTime FechaDocumento { get; set; }
        public DateTime FechaVencimiento { get; set; }
        public string Indicativo { get; set; }
        public string Naturaleza { get; set; }
        public string Nit { get; set; }
        public string OrdenFacturacion { get; set; }
        public int Plazo { get; set; }
        public string PrimerApellido { get; set; }
        public string PrimerNombre { get; set; }
        public string RazonSocial { get; set; }
        public double SaldoDocumento { get; set; }
        public string SegundoApellido { get; set; }
        public string SegundoNombre { get; set; }
        public string Telefono { get; set; }
        public string TieneRut { get; set; }
        public string TipoNit { get; set; }
        public Transaccion TipoTransaccion { get; set; }
        public string Compania { get; set; }

this is the enum

public enum Transaccion
        Ninguna = 0,
        OtrasCxP = 9,
        Compra = 10,
        NDCompras = 11,
        NCCompras = 12,
        NDOtrasCxP = 13,
        NCOtrasCxP = 14,
        TransladosEntreBodegas = 15,
        OtrasEntradas = 16,
        OtrasSalidas = 17,
        EntradasMercanciaConsignacion = 18,
        SalidadasMercanciaConsignacion = 19,
        ConsumosDonacion = 20,
        AnulacionConsumosDonacion = 21,
        Venta = 30,
        VentasMostrador = 31,
        NCVentas = 33,
        NDVentas = 34,
        NDChequesDev = 40,
        NCChequesDev = 41,
        NDCargosVarios = 42,
        NCAbonosVarios = 43,
        AnticipoCxC = 44,
        NDInteresMora = 45,
        NCBanco = 70,
        NDBanco = 71,
        Cheques = 72,
        Consignaciones = 73,
        TrasladosBancarios = 74,
        AnticipoCxP = 75,
        ChequesAnulados = 76,
        ReciboCaja = 90,
        AnulacionReciboCaja = 91,
        CostosProduccion = 95
                Do you have to serialize to a string?  Depending on what you're doing with the data, you might be able to stream the JSON directly to its destination.
– user5090812
                Jan 19, 2016 at 18:55

Circular reference could lead to OutOfMemory Exception. Try to check this is not the case because I ran into that exception a couple of times.

Where we have an item in a list which points to an item which in turn points to the first item in the list, thus leading to infinite loop in the serialization process.

Update:

You can ignore circular reference by updating your code like so:

str = JsonConvert.SerializeObject(list, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore catch(Exception ex) throw ex; please see update, I dont think it can be circular reference, the class has only native types, not reference to other types – Luis Valencia Jan 19, 2016 at 19:50 I tried out your code by creating 400 objects in memory and I didn't get any errors. Try to restart your computer and try again. Also check that you are using the latest JSON library. – Oluwafemi Jan 20, 2016 at 9:14

Like @Oluwafemi said you might have Circular Reference. You can check it by using NDepend Detect Dependency Tool.

You can also might read more by Find out the size of a .net object

please see update, I dont think it can be circular reference, the class has only native types, not reference to other types – Luis Valencia Jan 19, 2016 at 19:50 What is Transaccion object? How does it look? Also you can use my second part of the answer to check how big is the size of your object. – Vlad Bezden Jan 19, 2016 at 19:57

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.