SIGKILL while allocating memory in C++
Asked Answered
B

2

11

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?

Barnstorm answered 2/8, 2012 at 14:0 Comment(2)
I'm not sure, but you can try to use "new (nothrow)" cplusplus.com/reference/std/new/nothrowPopliteal
I do in most of the cases, surely when I'm allocating bigger amounts of memory. In some cases I was too lazy to rewrite new to new(std::nothrow) but I suppose the process would terminate with exception instead of SIGKILL.Barnstorm
P
17

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 (details/solution are specific to the Linux kernel):

  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

Plenipotentiary answered 2/8, 2012 at 15:0 Comment(2)
You begin with being not sure what kind of OS the OP is using, and end with a Linux-specific Solution. Would be better to either assume Linux from the beginning, or to warn about the solution being Linux-specific.Montero
@Ruslan, You are right. I added a hint about that.Plenipotentiary
P
1

Two ideas come to mind.

  1. Write your own memory allocation function rather than depending on new directly. You mentioned you're on an embedded system, where special allocators are quite common in applications. Are you running your application directly on the hardware or are you running in a process under an executive/OS layer? If the latter, is there a system API provided for allocating memory?

  2. Check out C++ set_new_handler and see if it can help you. You can request that a special function is invoked when a new allocation fails. Perhaps in that function you can take action to prevent whatever is killing the process from executing. Reference: http://www.cplusplus.com/reference/std/new/set_new_handler/

Paederast answered 2/8, 2012 at 14:12 Comment(2)
add 1) Yes, this is a way how to workaround it but I was too lazy to go this way so far if there could be a system-configuration-based solution. It's Open Embedded distrubution so quite heavy weight comparing to "real" embedded systems. I don't know if there is any API for special memory allocations. Any hints? add 2) Great insight, I'll try that.Barnstorm
These approaches are unlikely to help, if the cause is OS overcommit. Black's answer addresses that.Hasheem

© 2022 - 2024 — McMap. All rights reserved.