0

I'll try to provide as many necessary details as possible. Please ask for more detail before voting me down.
We are trying to serialize an object that has a CollectionBase property called ObjList inside of it. (Sample of the classes involved below.)

When we call the Serialize function, we're using an XmlTextWriter to serialize the object, then use a StreamReader.ReadToEnd on the MemoryStream filled by the writer to read into a String object. At the point of trying to read from the memory stream, we get an 'Out of Memory' error.

The MemoryStream.Length shows a little over 200 million bytes. The object being serialized has 17922 objects in it's ObjList property (Type CollectionBase), and each of those objects has an ObjList with variable amounts of data in it.

Here is the Serialize Code:

Public Class XMLserializer(Of T)

    Public _serializer As New System.Xml.Serialization.XmlSerializer(GetType(T))

    '... Some other code  

    Public Function Serialize(ByVal myobject As T) As String
        'Reset local varaible to actual object type         
        _serializer = New System.Xml.Serialization.XmlSerializer(myobject.GetType)
        'serialise to a memory stream, then read into a string  
        Try
            Dim result As String = Nothing
            If myobject IsNot Nothing Then
                Using ms As New MemoryStream
                    Using xtw As New XmlTextWriter(ms, System.Text.Encoding.UTF8)
                        xtw.Formatting = Formatting.Indented
                        _serializer.Serialize(xtw, myobject)
                        'rewind  
                        ms.Seek(0, System.IO.SeekOrigin.Begin)
                        Using reader As New StreamReader(ms, Text.Encoding.UTF8)
                            result = reader.ReadToEnd()
                            xtw.Close()
                            reader.Close()
                        End Using
                    End Using
                End Using
            End If

            Return result

        Catch ex As Exception
            Throw
        End Try
    End Function

End Class

Here are my object definitions:

<Serializable()> _
Public Class SLMR_ObjList(Of T)
    Inherits Collections.CollectionBase
    Implements IList(Of T)

    '.... Properties and Methods

End Class

' Location objects
<Serializable()> _
Public Class LocationCollection

    Private _serializer As XMLserializer(Of LocationCollection) = Nothing

    Private _locationList As SLMR_ObjList(Of Location)
    Public Property LocationList As SLMR_ObjList(Of Location)
        Get 
            If _locationList Is Nothing Then
                _locationList = New SLMR_ObjList(Of Location)
            End If
            Return _locationList
        End Get
        Set(ByVal value As SLMR_ObjList(Of Location))
            _locationList = value
        End Set
    End Property

    Public Sub Search_EquipNotInspected() 'As String
        Dim ds As DataSet = Nothing
        Dim dt As DataTable = Nothing
        Try
            Me.ObjList.Clear()
            ds = DAO_AS400_ELVPLOC00.SearchForEquipNotInspected(_search_counties,
                                                     _search_fromdate,
                                                     _search_todate)
            dt = ds.Tables(0)
            If dt.Rows.Count > 0 Then
                For Each dr As DataRow In dt.Rows
                    Dim curLocation As AS400_ELVPLOC00
                    ' Check and load the current location, or create new entry  
                    curLocation = Me.ObjList.FindByProperty("ID", dr.Item("LocID"))
                    If IsNothing(curLocation) OrElse curLocation.ID = 0 Then 
                        curLocation = New AS400_ELVPLOC00
                        With curLocation
                            .ID = dr.Item("LocID")
                            .Address1 = dr.Item("Addr1")
                            ' ...  
                        End With
                        Me.ObjList.Add(curLocation)
                    End If

                    Dim curEquipment As New AS400_ELVPEQP00
                    With curEquipment
                        .ID = dr.Item("EquipID")
                        .ELBLDGCD = dr.Item("ELBLDGCD")
                        ' ...  
                    End With
                    curLocation.AS400_ELVPEQP_COL.ObjList.Add(curEquipment)
                Next
            End If
        Catch ex As Exception
            'TO DO:  Handle Exception  
        Finally
            If Not ds Is Nothing Then
                ds.Dispose()
            End If
        End Try
    End Sub

    Public Function XMLDATA() As String
        Return XMLSerialize(Me)
    End Function

    Public Shared Function XMLSerialize(ByVal myobject As LocationCollection) As String
        If _serializer Is Nothing Then
            _serializer = New XMLserializer(Of LocationCollection)
        End If
        Return _serializer.Serialize(myobject)
    End Function

End Class

<Serializable()> _
Public Class Location
    Private _id As Integer
    Public Property ID As Integer
        Get 
            Return _id 
        End Get
        Set(ByVal value As Integer
            _id = value
        End Set
    End Property

    Private _address1 As String
    Public Property Address1 As String
        Get 
            Return _address1 
        End Get
        Set(ByVal value As String
            _address1 = value
        End Set
    End Property

    '.... other values

    Private _equipCollection As EquipmentCollection
    Public Property EquipCol As EquipmentCollection
        Get 
            If _equipCollection Is Nothing Then
                _equipCollection = New EquipmentCollection
            End If
            Return _equipCollection 
        End Get
        Set(ByVal value As EquipmentCollection)
            _equipCollection = value
        End Set
    End Property
End Class

' Equipment objects  
<Serializable()> _
Public Class EquipmentCollection
    Private _equipmentList As SLMR_ObjList(Of Equipment)
    Public Property EquipmentList As SLMR_ObjList(Of Equipment)
        Get 
            If _equipmentList Is Nothing Then
                _equipmentList = New SLMR_ObjList(Of Equipment)
            End If
            Return _equipmentList 
        End Get
        Set(ByVal value As SLMR_ObjList(Of Equipment))
            _equipmentList = value
        End Set
    End Property
End Class

<Serializable()> _
Public Class Equipment
    Private _id As Integer
    Public Property ID As Integer
        Get 
            Return _id 
        End Get
        Set(ByVal value As Integer
            _id = value
        End Set
    End Property

    Private _equipManuf As String
    Public Property Manufacturer As String
        Get 
            Return _equipManuf 
        End Get
        Set(ByVal value As String
            _equipManuf = value
        End Set
    End Property

    Private _equipLocation As Location
    Public Property EquipLocation As Location
        Get 
            If _equipLocation Is Nothing Then
                _equipLocation = New Location
            End If
            Return _equipLocation 
        End Get
        Set(ByVal value As Location
            _equipLocation = value
        End Set
    End Property

End Class

We call it like this:

Dim locCollection As New LocationCollection
'.... set object's search properties  
locCollection.Search_EquipNotinspected() ' Gets 34002 records, which works out to 17922 Location objects  
Dim xml As String = locCollection.XMLDATA

With 34000 records, we get out of Memory errors. If we use < 2000 records, it works. Any thoughts on how to figure out why we get the errors?

7
  • Try / Catch Throw End Catch End Try does nothing. Get rid of it. Jan 8, 2015 at 15:30
  • FYI, you should not use New XmlTextReader() or New XmlTextWriter(). They have been deprecated since .NET 2.0. Use XmlReader.Create() or XmlWriter.Create() instead. Jan 8, 2015 at 15:32
  • Thank you. I am working with the XmlWriter.Create() now. Also, the Try/Catch gives me a spot to catch the thread during debugging. Is there a more efficient way of doing this? Jan 8, 2015 at 17:54
  • As I test, I took the main object, and walked the collection, XMLSerializing each of those objects. As I did, ad Appended them to a StringBuilder object. The StringBuilder length = 183324697, and when I try to do StringBuilder.ToString() I get the System.OutOfMemoryException, so it must just be too much data. But why? We're on a 32bit system... I thought I could get 2,147,483,647, or at least 1,073,741,823. What are the limits I'm actually working with here? Jan 8, 2015 at 17:59
  • I would think you could set the debugger to break on unhandled exceptions of the particular type. In any case, if you just put in a comment saying that it's for debug purposes, that would make me happy. Jan 8, 2015 at 18:02

0

Your Answer

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

Browse other questions tagged or ask your own question.