此代码导致错误
的System.OutOfMemoryException
Using VT As New BidiEntities
For Each dr In Bidis
Dim hkBidi As New hkBidi
hkBidi.UserId = Userx
VT.AddTohkBidis(hkBidi)
Next
*VT.SaveChanges()*
End Using
循环遍历500,000条记录时, VT.SaveChanges()
会导致此错误。
如何解决这个问题?
答案 0 :(得分:3)
如果你试图存储500,000条记录,并且这样做会出现内存不足异常,那么你需要重构你的循环以执行每x条记录的SaveChanges,其中x是一些不会导致异常的值(即10,000)。
这将使您的代码稍微复杂一些,但性能更高:
Dim wCount As Integer
Const MAX_LOOPS As Integer = 10000
Dim VT As BidiEntities
Try
For Each dr In Bidis
If VT Is Nothing Then
wCount = 0
VT = New BidiEntities
End If
Dim hkBidiAs New hkBidi
hkBidi.UserId = Userx
VT.AddTohkBidis(hkBidi)
wCount = wCount + 1
' When we reach the maximum number of loops, save the changes, then dispose the VT Object and set it to nothing
If wCount >= MAX_LOOPS Then
VT.SaveChanges()
VT.Dispose()
VT = Nothing
End If
Next
Catch
' Do something with the exception
Finally
' Handle the records added in the last loop, if any
If VT IsNot Nothing Then
VT.SaveChanges()
VT.Dispose()
VT = Nothing
End If
End Try
答案 1 :(得分:0)
Dim i As Integer
Dim uList As New List(Of hkBidi)
For Each dr In Bidis
i += 1
uList.Add(New hkBidi With {.UserId = dr.UserId})
If i > 9999 Then
i = 0
BidiSave(uList)
uList.Clear()
End If
Next
' Up to 500000 if the (sample 502500) and smaller than 10000
If uList.Any() Then
BidiSave(uList)
End If
-
Public Sub BidiSave(uList As List(Of hkBidi))
Using VT As New BidiEntities
For Each dr In uList
VT.AddTohkBidis(dr)
Next
VT.SaveChanges() ' No Problem
End Using ' No Exception
End Sub