Preventing OutOfMemoryException with GC.AddMemoryPressure()?

前端 未结 2 1629
你的背包
你的背包 2020-12-15 07:05

I\'m currently debugging a method we use to tag images with a certain text before displaying them in our system.

The tag method looks like this at the moment:

<
2条回答
  •  温柔的废话
    2020-12-15 07:46

    You are having a different problem here, this code uses very little memory. Sadly GDI+ exceptions are pretty crummy. Diagnose this with TaskMgr.exe, Processes tab. View + Select Columns and tick GDI Objects, Handles and USER Objects.

    If my suspicion is correct, you'll see the GDI Objects counter for your process climbing constantly as this code runs. When it reaches 10,000 Windows decides that there's something fundamentally wrong with the code and refuses to create any more handles. GDI+ then gets a bit gimpy about it and reports an out of memory error. Wrong, it should have been a 'could not create handle' error. An error code that it doesn't have. .NET is powerless to improve the exception.

    Anyhoo, the reason is that you are not calling Dispose() on the font and brush. Wrap them with the using statement. This normally doesn't cause trouble but your program is apparently using too little garbage collected memory to ever kick off the finalizer thread.

提交回复
热议问题