为什么我得到SIGKILL而不是std :: bad_alloc?

时间:2016-10-10 08:32:19

标签: c++ linux exception memory

我的16GB机器就像内存不足一样(应用程序报告分配失败,ksqwapd在没有安装交换分区的情况下变得疯狂),即使top等系统监视器报告有足够的可用内存

所以我编写了这个程序来为耗尽分配内存并报告分配的数量。

#include <iostream>
#include <stdexcept>

const size_t STRIDE = 64;

int main ()
{
    for (size_t n = 0; ; ++n)
    {
        try
        {
            char * ptr = new char [1024*1024];

            for (char * p = ptr; p < ptr + 1024*1024; p += STRIDE)
            {
                *p = 123;
            }

            if (n > 0 && 0 == (n % 1024))
            {
                std :: cout << (n / 1024) << "   GiB" << std :: endl;
            }
            else if (n > 0 && 0 == (n % 512))
            {
                std :: cout << (n / 1024) << ".5 GiB" << std :: endl;
            }
        }
        catch (const std :: exception & e)
        {
            std :: cout << n << "MiB total" << std :: endl;
            std :: cout << "Caught: " << e .what () << std :: endl;
        }
        catch (...)
        {
            std :: cout << n << "MiB total" << std :: endl;
            std :: cout << "Caught exception." << std :: endl;
        }
    }

    return 0;
}

当我使用超过10GiB的报告可用内存运行此程序时,它在计入2GiB后收到SIGKILL。为什么它会收到信号而不是抛出异常?

另外,任何想法为什么它只到2GiB?我重新启动后再次运行它,它达到13GiB(它应该有13.5GiB,因为top报告13.9GiB可用)。什么可能会阻止所有可用内存被授予?

Ubuntu,内核4.2.0-42-generic。

**编辑:* / proc / sys / vm / overcommit_memory为0(启发式过度使用),但这并不能解释为什么其他程序似乎捕获内存不足的原因例外而非被杀。

0 个答案:

没有答案