0

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.

 try
 {
    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
    }
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

2 Answers2

2

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:

 try
 {
    str = JsonConvert.SerializeObject(list, Formatting.Indented, 
                            new JsonSerializerSettings { 
                                   ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
                            });
 }
 catch(Exception ex)
 {
    throw ex;
 }
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
  • 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 '16 at 19:50
  • What is in `Transaccion` and do you still get the same error with the code above? – Oluwafemi Jan 19 '16 at 19:56
  • see uodate, and yes i get the same exception with code above – Luis Valencia Jan 19 '16 at 22:04
  • 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 '16 at 09:14
0

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

Community
  • 1
  • 1
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • 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 '16 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 '16 at 19:57