使用 Python 吃内存 [英] Eat memory using Python
问题描述
我正在尝试创建一个可以有目的"的应用程序消耗我们立即指定的 RAM.例如我想消耗512 MB RAM,那么应用程序将直接消耗512 MB.
I am trying to create an app that can "Purposely" consume RAM as much as we specify immediately. e.g. I want to consume 512 MB RAM, then the app will consume 512 MB directly.
我在网上搜索过,他们中的大多数都使用while循环来用变量或数据填充ram.但我认为填充 RAM 的方法很慢,而且可能也不准确.
I have search on the web, most of them are using while loop to fill the ram with variable or data. But I think it is slow way to fill the RAM and might not accurate either.
我正在寻找一个关于内存管理的 Python 库.并遇到了这些 http://docs.python.org/library/mmap.html.但是不知道怎么用这些库一口气吃掉RAM空间.
I am looking for a library in python about memory management. and came across these http://docs.python.org/library/mmap.html. But can't figure out how to use these library to eat the RAM Space in one shot.
我曾经看到过 mem-eater 应用程序,但不知道它们是如何编写的...
I ever saw an mem-eater application, but don't know how they were written...
那么,是否有其他更好的建议立即用随机数据填充 RAM?或者我应该只使用 while 循环手动填充数据但使用多线程来使其更快?
So, is there any other better suggestion for Fill the RAM with random data immediately? Or Should I just use while loop to fill the data manually but with Multi-Threading to make it faster?
推荐答案
一种简单的方法可能是:
One simple way might be:
some_str = ' ' * 512000000
在我的测试中似乎运行良好.
Seemed to work pretty well in my tests.
编辑:在 Python 3 中,您可能希望使用 bytearray(512000000)
代替.
Edit: in Python 3, you might want to use bytearray(512000000)
instead.
这篇关于使用 Python 吃内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!