vb.net托管内存和堆是稳定的,但进程内存使用会导致崩溃

问题描述 投票:0回答:1

我有一个应用程序读取带有图像名称的csv文件,调用打开图像的子,扫描以找到形状中的黄色像素,并解析左上角,注释csv数据并写入记录。托管堆和本机堆内存稳定,但每个映像的进程内存增加350MB。现在我让我的分析师将csv文件分成25个图像集。但对于20位分析师而言,这是冒险的。

我处理图像,尝试GCCollect(),大堆压缩 - 似乎没有任何帮助。我已阅读了大部分帖子,似乎没有任何内容。我已经包含了代码 - 并尝试去除显示和垃圾,但留下了一些扫描比较。

我正在运行.net 4.6.1,Win 10 / 64,16GB内存

Sub Process_CSV_Detail()。 。 。顺序读取csv文件,设置文件名,显示给用户,而不是每个图像的调用工作过程 - 每次访问新图像时都会发生内存泄漏

    Call BackgroundProcess2()

End Sub

Public Sub BackgroundProcess2()

        GreenTest = 245
        RedTest = 245
        BlueTest = 70

    Try
        loadedImage = Image.FromFile(InputImageName)
    Catch ex As Exception
         . . . .Never gets here but some code 
    Finally
    End Try

    HeightVal = loadedImage.Height
    WidthVal = loadedImage.Width

    Dim MasterImage As New Bitmap(WidthVal, HeightVal, FormatVal)

    MasterImage = loadedImage

。 。 。现在正在寻找那个讨厌的像素

    For ycounter = 1 To HeightVal - 1 Step PixelStride
        For xcounter = 1 To WidthVal - 1 Step PixelStride

            Dim PixelColor As Color = MasterImage.GetPixel(xcounter, ycounter)
            PixelIsYellow = False


                If PixelColor.R > RedTest Then
                    If PixelColor.G > GreenTest Then
                        If PixelColor.B < BlueTest Then
                            PixelIsYellow = True
                            YellowPixelCount = YellowPixelCount + 1
                            MasterImage.SetPixel(xcounter, ycounter, Color.FromArgb(&HFFFFFF00))

                            xPixelIsYellow = True
                            yPixelIsYellow = True

                        End If
                    End If
                End If




            If PixelIsYellow = True Then
                'Now  find the upper left corner
                LeftXLoc = xcounter
                LeftYLoc = ycounter
                'Move to left until no more yellow, then back 1 step to 
                      'locate left pixel location
                Try
                    For xtestcounter = LeftXLoc To 1 Step -1
                        Dim PixelColorx As Color = MasterImage.GetPixel(xtestcounter, LeftYLoc)
                        If PixelColorx.R < RedTest Then
                            xPixelIsYellow = False
                            Exit Try
                        End If
                        If QA_Mode = False Then
                            If PixelColorx.G < GreenTest Then
                                xPixelIsYellow = False
                                Exit Try
                            End If
                        End If
                        If QA_Mode = True Then
                            If PixelColorx.G < GreenTest Then
                                xPixelIsYellow = False
                                Exit Try
                            End If
                        End If


                        If PixelColorx.B > 70 Then
                            xPixelIsYellow = False
                            Exit Try
                        End If

                    Next
                Catch ex As Exception
                Finally

                End Try
                LeftXLoc = xtestcounter + 1
                'Move up until no more yellow, then back 1 step to locate left pixel location
                Try
                    For ytestcounter = LeftYLoc To 1 Step -1
                        Dim PixelColory As Color = MasterImage.GetPixel(LeftXLoc, ytestcounter)
                        If PixelColory.R < RedTest Then
                            yPixelIsYellow = False
                            Exit Try
                        End If

                        If PixelColory.G < GreenTest Then
                            yPixelIsYellow = False
                            Exit Try
                        End If
                        If PixelColory.B > BlueTest Then
                            xPixelIsYellow = False
                            Exit Try
                        End If

                    Next
                Catch ex As Exception
                    MsgBox("Error in locating upper left pixel")
                Finally
                End Try
                LeftYLoc = ytestcounter + 1
                OutputLine = CurrentDataLine & "," & xcounter & "," & ycounter & "," & LeftXLoc & "," & LeftYLoc
                PrintLine(TargetFileNumber, OutputLine)
            End If
        Next
    Next
    loadedImage.Dispose()
    MasterImage.Dispose()
    ' - I have tried these but no effect 
    'GC.Collect()
    'Runtime.GCSettings.LargeObjectHeapCompactionMode = Runtime.GCLargeObjectHeapCompactionMode.CompactOnce
    'Finalize()
    End Sub

我希望有人会拥有一个允许过程记忆稳定的银弹 - 我试过ANTS但没有快乐。

vb.net memory-leaks gdi+
1个回答
1
投票

这两行是(至少部分)问题:

Dim MasterImage As New Bitmap(WidthVal, HeightVal, FormatVal)

MasterImage = loadedImage

您使用指定的尺寸和像素格式创建新的位图,然后立即将MasterImage变量对新位图的引用替换为loadedImage的引用。现在新的位图没有任何引用,因此在您关闭进程之前,它不会被释放并因此存在于内存中。相反,MasterImage现在引用与loadedImage完全相同的位图。

因此,当您的代码处理位图时,它实际上是尝试两次处理相同的位图:

loadedImage.Dispose()
MasterImage.Dispose() 'This is the exact same bitmap as loadedImage, which is already disposed of.

GDI +中的图像数据是未损坏的内存,这就是托管内存图保持稳定的原因。非托管内存只是说任何不是垃圾收集器(GC)管理的内存,这就是为什么调用它的任何方法都无济于事。它必须由程序员手动释放(在这种情况下通过调用Dispose())。

解决方案是根本不创建新的位图,因为你从未真正使用它。完全删除MasterImage变量并直接在loadedImage上运行。

© www.soinside.com 2019 - 2024. All rights reserved.