人为限制 C/C++ 内存使用 [英] Artificially Limit C/C++ Memory Usage

查看:58
本文介绍了人为限制 C/C++ 内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以轻松地将 C/C++ 应用程序限制为指定的内存量(30 mb 左右)?例如:如果我的应用程序尝试将 50mb 文件完全加载到内存中,它将死/打印一条消息并退出/等.

Is there any way to easily limit a C/C++ application to a specified amount of memory (30 mb or so)? Eg: if my application tries to complete load a 50mb file into memory it will die/print a message and quit/etc.

诚然,我可以不断地检查应用程序的内存使用情况,但如果我继续检查它会因错误而死亡,那会容易一些.

Admittedly I can just constantly check the memory usage for the application, but it would be a bit easier if it would just die with an error if I went above.

有什么想法吗?

平台不是大问题,windows/linux/任何编译器.

Platform isn't a big issue, windows/linux/whatever compiler.

推荐答案

阅读 unix 系统上 ulimit 的手册页.您可以在启动可执行文件或(在手册的第 3 节中)调用同名 API 之前调用内置的 shell.

Read the manual page for ulimit on unix systems. There is a shell builtin you can invoke before launching your executable or (in section 3 of the manual) an API call of the same name.

这篇关于人为限制 C/C++ 内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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