如何捕捉一个OutOfMemoryException工作? [英] How does catching an OutOfMemoryException work?

查看:173
本文介绍了如何捕捉一个OutOfMemoryException工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到有点困惑的事实,我们就可以赶上一个 OutOfMemoryException异常使用try / catch块。

I am a little bit confused about the fact that we can just catch an OutOfMemoryException using a try/catch block.

考虑下面的代码:

Console.WriteLine("Starting");

for (int i = 0; i < 10; i++)
{
    try
    {
        OutOfMemory();
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.ToString());
    } 
}

try
{
    StackOverflow();
}
catch (Exception exception)
{
    Console.WriteLine(exception.ToString());
}

Console.WriteLine("Done");



我用来创建内存不足+ StackOverflowException方法:

The methods I used to create the OutOfMemory + StackOverflowException:

public static void OutOfMemory()
{
    List<byte[]> data = new List<byte[]>(1500);

    while (true)
    {
        byte[] buffer = new byte[int.MaxValue / 2];

        for (int i = 0; i < buffer.Length; i++)
        {
            buffer[i] = 255;
        }

        data.Add(buffer);
    }
}

static void StackOverflow()
{
    StackOverflow();
}



据打印出 OutOfMemoryException异常 10次,然后终止由于 StackOverflowException ,它不能处理的。

It prints out the OutOfMemoryException 10 times and then terminates due to the StackOverflowException, which it can't handle.

的RAM图看起来像在执行程序:

The RAM graph looks like that while executing the program:

我现在的问题是我们为什么能够捕获 OutOfMemoryException异常?捕获它之后,我们可以去上执行任何我们想要的代码。它见证了RAM图,有内存释放。如何在运行时知道哪些对象可以GC和它仍然需要继续执行?

My question now it why are we able to catch the OutOfMemoryException? After catching it we can just go on execute any code we want. As proven by the RAM graph, there is memory released. How does the runtime know which objects it can GC and which are still required for further execution?

推荐答案

在GC使得上分析这是在程序中使用,并且引用可以扔掉未在任何地方使用任何对象。

The GC makes an analysis on the references that are used in the program, and can throw away any object that isn't used anywhere.

这是 OutOfMemoryException异常并不意味着内存完全耗尽,它只是意味着内存分配失败。如果你试图分配一次大的内存区域,可能仍有足够的剩余空间。

An OutOfMemoryException doesn't mean that the memory is completely depleted, it just means that a memory allocation failed. If you tried to allocate a large memory area at once, there may still be plenty of free memory left.

当没有一个分配足够的可用内存,系统做一个垃圾收集,试图释放内存。如果仍然没有足够的内存来分配,它会抛出异常。

When there isn't enough free memory for an allocation, the system does a garbage collection to try to free up memory. If there still isn't enough memory for the allocation, it will throw the exception.

A StackOverflowException 不是可以处理,因为这意味着堆栈已满,并且它不可能从它删除任何东西,因为它是与堆。你会需要更多的堆栈空间继续运行,将处理异常的代码,但没有更多的。

A StackOverflowException is not possible to handle, because it means that the stack is full, and it's not possible to remove anything from it as it is with the heap. You would need more stack space to continue running the code that would handle the exception, but there is no more.

这篇关于如何捕捉一个OutOfMemoryException工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆