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
    }
4
  • What happens if you serialize only a few items of that list? What does it generate? Commented Jan 19, 2016 at 18:52
  • 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. Commented Jan 19, 2016 at 18:55
  • will try to serialize the list with one item Commented Jan 19, 2016 at 18:59
  • What is the size of each object? Can you show how the class looks like? Commented Jan 19, 2016 at 18:59

2 Answers 2

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;
 }
Sign up to request clarification or add additional context in comments.

4 Comments

please see update, I dont think it can be circular reference, the class has only native types, not reference to other types
What is in Transaccion and do you still get the same error with the code above?
see uodate, and yes i get the same exception with code above
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.
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

2 Comments

please see update, I dont think it can be circular reference, the class has only native types, not reference to other types
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.