开发者

Why I got System.OutOfMemoryException when using this code?

开发者 https://www.devze.com 2023-03-17 17:56 出处:网络
Dim output1 = System.IO.File.ReadAllLines(file).ToStrin开发者_JS百科g The file size is only 1 GB. My Page file is 128 GB. Why out of memory? It\'s 64bit system. Because there are many different fact
Dim output1 = System.IO.File.ReadAllLines(file).ToStrin开发者_JS百科g

The file size is only 1 GB. My Page file is 128 GB. Why out of memory? It's 64bit system.


Because there are many different factors that go into OOM exceptions. Start with "Out Of Memory" Does Not Refer to Physical Memory.

An "out of memory" error almost never happens because there’s not enough storage available... Rather, an "out of memory" error happens because the process is unable to find a large enough section of contiguous unused pages in its virtual address space to do the requested mapping.

You should also read CLR Inside Out - Investigating Memory Issues.

The process can run out of virtual space if virtual memory is overly fragmented.

So do not just look at your file size verses your pagefile, you need to examine what else your application is doing and even what other processes are running on the system.


I am a novice but I use this snippet in some of my more memory-intensive apps:


Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal process As IntPtr, ByVal minimumWorkingSetSize As Integer, ByVal maximumWorkingSetSize As Integer) As Integer
Private Shared Sub _FlushMemory()
    Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
        If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
            SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
            Dim myProcesses As Process() = Process.GetProcessesByName(My.Application.Info.AssemblyName)
            Dim myProcess As Process
            For Each myProcess In myProcesses
                SetProcessWorkingSetSize(myProcess.Handle, -1, -1)
            Next myProcess
        End If
    Catch ex As Exception
    End Try
End Sub


Just noticed also what you wrote. you can't convert String Array (ReadAllLines) to a String.

Try this:

Dim output1 As String = IO.File.ReadAllText(file)

This returns the contents of the file as a String. If this is what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消