Create a variable with n bytes

Viewed 78

I'm writing an application that works with big-data applications. One of my processes is failing and debugging is taking a while because some of the files that needed to be loaded in memory are 50+ gb.

I want to be able to create an object with 2GB and another with 41GB of memory space. (It doesn't matter what is in the object, the object just needs to take some memory). I would read from a file, but its is taking a while.

Is there a programmatic way of creating a object with 2GB of memory size in python3.7?

2 Answers
import sys

a = 'a' * 3000000000
sys.getsizeof(a) / 1024 / 1024 / 1024

The output is 2.7939677694812417 = 2.8GB.

Thanks to @Quang Hoang in the comments. I'm just adding this answer here if someone is looking for an answer

some_var = 'x' * (1<<32)
Related