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
–
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;
–
–
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
–
–
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.