【C++】std::set_new_handler使用场景

std::set_new_handler使用场景

C++的STL提供了std::set_new_handler,new操作分配内存失败时,会先调用这个Handler。
默认的new_handler行为是,抛出std::bad_alloc异常。

std::new_handler原型:

// (until C++11)
std::new_handler set_new_handler( std::new_handler new_p ) throw();
// (since C++11)
std::new_handler set_new_handler( std::new_handler new_p ) noexcept;

cppreference官网中对其说明如下

The new-handler function is the function called by allocation
functions whenever a memory allocation attempt fails. Its intended
purpose is one of three things:

  1. make more memory available,
  2. terminate the program (e.g. by calling std::terminate)
  3. throw exception of type std::bad_alloc or derived from std::bad_alloc.

The default implementation throws
std::bad_alloc. The user can install their own new-handler, which may
offer behavior different than the default one.

allocation function 重复尝试分配内存,每次失败的时候都会调用new-handler,直到new-handler中调用了std::set_new_handler(nullptr),然后会抛出 std::bad_alloc异常。

If new-handler returns, the allocation function repeats the
previously-failed allocation attempt and calls the new-handler again
if the allocation fails again. To end the loop, new-handler may call
std::set_new_handler(nullptr): if, after a failed allocation attempt,
allocation function finds that std::get_new_handler returns a null
pointer value, it will throw std::bad_alloc.

  • 小例
#include <iostream>
#include <new>
 
void handler()
{
    std::cout << "Memory allocation failed, terminating\n";
    std::set_new_handler(nullptr);
}
 
int main()
{
    std::set_new_handler(handler);

    while (true)
    {
        new int[1000'000'000ul]();
    }

}

使用场景

  • 记录异常事件:通过std::set_new_handler设置内存不足时的new-handler,当检测到内存异常时,通过日志系统进行记录(PS:此时需要考虑日志系统是否可用)

  • 预先分配一段内存并在异常时释放内存尝试自救:运行初期,事先分配一段内存池。当内存不足new失败时,释放这段内存进行自救(保证系统可最低限度的运行正常)。同时,通知系统异常消息,让用户进行重启、Reset等操作。

  • 设计LowMemory:系统低内存时,进程自救策略。比如Kill掉一些不重要的进程,主动关闭系统某些功能

实际上,Linux、Android等系统,都有其完善的内存策略,如交换分区、LowMemeory Killer、Kenerl Panic等等。并且实际开发时,会有完善的流程(设计、review),很少出现单纯因为某些程序重复new导致内存不足,而触发new-handler。
因为在触发之前,常规系统的lowmemory killer已经把异常进程kill掉了。

相关推荐

  1. Redis使用场景(五)

    2024-03-31 16:28:08       46 阅读
  2. redis常见使用场景

    2024-03-31 16:28:08       8 阅读
  3. capm模型使用场景

    2024-03-31 16:28:08       12 阅读
  4. redis常见使用场景

    2024-03-31 16:28:08       9 阅读
  5. TCP&&UDP使用场景讨论

    2024-03-31 16:28:08       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-31 16:28:08       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-31 16:28:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-31 16:28:08       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-31 16:28:08       20 阅读

热门阅读

  1. springboot3.2.4+Mybatis-plus在graalvm21环境下打包exe

    2024-03-31 16:28:08       17 阅读
  2. nginx 常用功能

    2024-03-31 16:28:08       15 阅读
  3. 蓝桥杯2014年第十三届省赛真题-武功秘籍

    2024-03-31 16:28:08       22 阅读
  4. Nginx配置文件解释

    2024-03-31 16:28:08       14 阅读
  5. 数据结构 第6章 图(一轮习题总结)

    2024-03-31 16:28:08       11 阅读
  6. 嵌入式开发中观察者模式实现

    2024-03-31 16:28:08       18 阅读
  7. 简化数据迁移:API接口的应用

    2024-03-31 16:28:08       18 阅读
  8. PostCSS安装与基本使用

    2024-03-31 16:28:08       19 阅读
  9. 自然语言处理:大模型LLM论文整理

    2024-03-31 16:28:08       16 阅读