Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eat memory using Python

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.

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.

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...

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?

like image 836
Yeo Avatar asked Jun 11 '11 18:06

Yeo


People also ask

Does Python consume a lot of memory?

In fact, Python uses more like 35MB of RAM to store these numbers. Why? Because Python integers are objects, and objects have a lot of memory overhead.

Can Python manipulate memory?

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager.


2 Answers

One simple way might be:

some_str = ' ' * 512000000 

Seemed to work pretty well in my tests.

Edit: in Python 3, you might want to use bytearray(512000000) instead.

like image 112
Jasmijn Avatar answered Sep 20 '22 15:09

Jasmijn


You won't be able to allocate all the memory you can using constructs like

s = ' ' * BIG_NUMBER 

It is better to append a list as in

a = [] while True:     print len(a)     a.append(' ' * 10**6) 

Here is a longer code which gives more insight on the memory allocation limits:

import os import psutil  PROCESS = psutil.Process(os.getpid()) MEGA = 10 ** 6 MEGA_STR = ' ' * MEGA  def pmem():     tot, avail, percent, used, free = psutil.virtual_memory()     tot, avail, used, free = tot / MEGA, avail / MEGA, used / MEGA, free / MEGA     proc = PROCESS.get_memory_info()[1] / MEGA     print('process = %s total = %s avail = %s used = %s free = %s percent = %s'           % (proc, tot, avail, used, free, percent))  def alloc_max_array():     i = 0     ar = []     while True:         try:             #ar.append(MEGA_STR)  # no copy if reusing the same string!             ar.append(MEGA_STR + str(i))         except MemoryError:             break         i += 1     max_i = i - 1     print 'maximum array allocation:', max_i     pmem()  def alloc_max_str():     i = 0     while True:         try:             a = ' ' * (i * 10 * MEGA)             del a         except MemoryError:             break         i += 1     max_i = i - 1     _ = ' ' * (max_i * 10 * MEGA)     print 'maximum string allocation', max_i     pmem()  pmem() alloc_max_str() alloc_max_array() 

This is the output I get:

process = 4 total = 3179 avail = 2051 used = 1127 free = 2051 percent = 35.5 maximum string allocation 102 process = 1025 total = 3179 avail = 1028 used = 2150 free = 1028 percent = 67.7 maximum array allocation: 2004 process = 2018 total = 3179 avail = 34 used = 3144 free = 34 percent = 98.9 
like image 36
markolopa Avatar answered Sep 19 '22 15:09

markolopa