SIGKILL while allocating memory in C++

前端 未结 2 1742
心在旅途
心在旅途 2020-12-03 21:44

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

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 22:34

    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

提交回复
热议问题