如何使用Python吞吃内存? [英] How to Eat Memory using Python?

查看:234
本文介绍了如何使用Python吞吃内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了实验,很有趣... 我正在尝试创建一个应用程序,该应用程序可以有目的地"消耗我们立即指定的尽可能多的RAM. 例如我要消耗512 MB的RAM,那么该应用程序将直接消耗512 MB.

Just for experiment, and Fun... 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.

我曾经见过吃内存的应用程序,但不知道它们是如何编写的...

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屋!

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