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

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

问题描述

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

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.

问题在于,如果内存完全耗尽,系统会通过 SIGKILL 杀死进程.我能以某种方式告诉 new 应该只返回 NULL 而不是终止进程吗?

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?

推荐答案

我不确定您使用的是哪种操作系统,但您应该检查一下它支持机会主义内存分配就像Linux一样.

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

如果启用,可能会发生以下情况(详细信息/解决方案特定于 Linux 内核):

If it is enabled, the following may happen (details/solution are specific to the Linux kernel):

  1. 您的 newmalloc 从内核获取有效地址.即使没有足够的内存,因为...
  2. 内核直到第一次访问的那一刻才真正分配内存.
  3. 如果所有的overcommitted"内存被使用,操作系统没有机会只能杀死所涉及的进程之一.(告诉程序内存不足为时已晚.)在Linux中,这称为内存不足杀(OOM 杀).此类杀死会记录在内核消息缓冲区中.
  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.

解决方案:禁用内存过度使用:<代码>回声 2 >/proc/sys/vm/overcommit_memory

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

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

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