2012-05-15 46 views
0

我正在使用twitter api數據,並將流結果存儲在文本文件中後,我將數據輸入到解析器應用程序中。我計劃的是大數據文件,所以我使用分隔符來讀取內容]}來分隔各個帖子以避免發生錯誤的可能性?備份功能是使用緩衝區讀取數據,然後剪切成單個帖子。 但問題是,在某些情況下,對於單個帖子,會發生內存異常。現在,當我查看單個帖子時,它看起來不是很大,但文本將包含外部字符或一些編碼,我猜這會導致內存異常。我還沒有想出如果正是這樣,但還沒有想到我會得到一些在這裏輸入或意見...outofmemory異常從文件中讀取xml

 myreader.TextFieldType = FileIO.FieldType.Delimited 
     myreader.SetDelimiters("]}}") 
     Dim currentRow As String() 

     Try 

      While Not myreader.EndOfData 
       Try 
        currentRow = myreader.ReadFields() 
        Dim currentField As String 

        For Each currentField In currentRow 
         data = data + currentField 
         counter += 1 
         If counter = 1000 Then 
          Dim pt As New parsingUtilities 
          If Not data = "" Then 
           pt.getNodes(data) 
           counter = 0 
          End If 
         End If 
        Next 
       Catch ex As Exception 
        If ex.Message.Contains("MemoryException") Then 
         fileBKup() 
        End If 
       End Try 

當內存出現異常時,其他時間然後我試圖分裂成不同的職位:

Dim sampleResults() As String 
    Dim stringSplitter() As String = {"}}"} 

    ' split the file content based on the closing entry tag 
    sampleResults = Nothing 
    Try 
     sampleResults = post.Split(stringSplitter, StringSplitOptions.RemoveEmptyEntries) 

    Catch ex As Exception 
     appLogs.constructLog(ex.Message.ToString, True, True) 
     moveErrorFiles(form1.infile) 
     Exit Sub 
    End Try 

回答

1

我希望問題是字符串。

字符串是不可變的,這意味着每次你覺得時間你在做這個

data = data + currentField 

你實際上是在內存中創建另一個新的字符串改變的字符串。所以如果你這樣做了幾千次,它會導致一個問題,因爲它們會加載並且你會得到一個OutOfMemoryException。

如果你正在建立字符串,你應該使用StringBuilder來代替。