【发布时间】:2019-04-01 16:31:44
【问题描述】:
我有一个应用程序,它读取带有图像名称的 csv 文件,调用打开图像的子程序,扫描以查找形状中的黄色像素,并解析左上角,注释 csv 数据并写入记录.托管堆和本机堆内存稳定,但每个映像的进程内存增长 350MB。现在我让我的分析师将 csv 文件切成 25 个图像集。但这对于 20 岁左右的分析师来说是有风险的。
我处理了图像,尝试了 GCCollect(),大型 heapCompaction - 似乎没有任何帮助。我已经阅读了大多数帖子,但似乎没有任何响动。我已经包含了代码 - 并试图去除显示和垃圾,但留下了一些扫描比较。
我正在运行 .net 4.6.1,Win 10/64,16GB 内存
子流程_CSV_Detail() . . .按顺序读取 csv 文件,设置文件名,为用户显示,然后为每个图像调用工作进程 - 访问每个新图像时似乎都会发生内存泄漏
Call BackgroundProcess2()
End 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+