如何模拟内存分配错误 [英] How to simulate memory allocation errors

查看:91
本文介绍了如何模拟内存分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C应用程序使用了3库,做自己的内存管理。
为了成为强大的,我的应用程序有code,处理由于缺乏可用内存的库函数失败。

My C application uses 3rd libraries, which do their own memory management. In order to be robust, my application has code to deal with failures of library functions due to lack of free memory.

我想测试这个code,为此,我需要模拟故障由于内存不足。

I would like to test this code, and for this, I need to simulate failures due to lack of memory.

什么工具/ s的建议呢?
我的环境是Linux的/ gcc的。

What tool/s are recommended for this? My environment is Linux/gcc.

推荐答案

您可以使用的ulimit 来限制资源的用户可以使用,包括内存量。所以你创建一个测试用户,限制其内存使用的东西就足以启动程序,然后看着它死了:)

You can use ulimit to limit the amount of resources a user can use, including memory. So you create a test user, limit their memory use to something just enough to launch your program, and watch it die :)

例如:

ulimit -m 64

设定64KB的内存限制。

Sets a memory limit of 64kb.

这篇关于如何模拟内存分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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