SIGKILL同时在C ++中分配内存 [英] SIGKILL while allocating memory in C++

查看:219
本文介绍了SIGKILL同时在C ++中分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++开发具有有限内存的嵌入式系统(Tegra 2)的应用程序。我在处理整个代码中的 new NULL结果,但有时会发生,但应用程序能够处理这。



问题是,如果内存用完了,系统会通过SIGKILL杀死进程。我可以以某种方式告诉 new 应该只返回NULL,而不是杀死进程?

解决方案

我不知道你使用什么样的操作系统,但你应该检查
是否支持

如果它是一个服务器管理/可能会发生以下情况:


  1. 您的 malloc 从内核获取有效的地址。 即使没有足够的内存,因为...

  2. 内核直到第一次访问时才真正分配内存。

  3. 如果使用了所有的过度使用内存,操作系统就没有机会杀死所涉及的进程之一。 (告诉程序没有足够的内存太晚了。)在Linux中,这被称为内存不足(OOM Kill)


  4. 解决方案:禁用内存过量使用:

    echo 2> / proc / sys / vm / overcommit_memory


    I'm developing application for an embedded system with limited memory (Tegra 2) in C++. I'm handling NULL results of new and new[] throughout the code which sometimes occurs but application is able to handle this.

    The problem is that the system kills the process by SIGKILL if the memory runs out completely. Can I somehow tell that new should just return NULL instead of killing the process?

    解决方案

    I am not sure what kind of OS You are using, but You should check if it supports opportunistic memory allocation like Linux does.

    If it is enabled, the following may happen:

    1. Your new or malloc gets a valid address from the kernel. Even if there is not enough memory, because ...
    2. The kernel does not really allocate the memory until the very moment of the first access.
    3. If all of the "overcommitted" memory is used, the operating system has no chance but killing one of the involved processes. (It is too late to tell the program that there is not enough memory.) In Linux, this is called Out Of Memory Kill (OOM Kill). Such kills get logged in the kernel message buffer.

    Solution: Disable overcommitting of memory: echo 2 > /proc/sys/vm/overcommit_memory

    这篇关于SIGKILL同时在C ++中分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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