小编Man*_*sen的帖子

使用GC.AddMemoryPressure()防止OutOfMemoryException?

我正在调试一个方法,我们用它来标记具有特定文本的图像,然后在我们的系统中显示它们.

标签方法目前看起来像这样:

private static Image TagAsProductImage(Image image)
{
    try
    {
        // Prepares the garbage collector for added memory pressure (500000 bytes is roughly 485 kilobytes).
        // Should solve some OutOfMemoryExceptions.
        GC.AddMemoryPressure(500000);

        using (Graphics graphics = Graphics.FromImage(image))
        {
            // Create font.
            Font drawFont = new Font("Tahoma", image.Width*IMAGE_TAG_SIZE_FACTOR);

            // Create brush.
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create rectangle for drawing.
            RectangleF drawRect = new RectangleF(0, image.Height - drawFont.GetHeight(), image.Width,
                                                    drawFont.GetHeight());

            // Set format of string to be right-aligned.
            StringFormat drawFormat = …
Run Code Online (Sandbox Code Playgroud)

c# garbage-collection out-of-memory

16
推荐指数
2
解决办法
6909
查看次数

标签 统计

c# ×1

garbage-collection ×1

out-of-memory ×1