2

I am handling large arrays with Python and the library numpy. Sometimes I create unexpectedly huge arrays, which fills totally the main memory (RAM) of the computer, and starts writing on the slow second memory (swap, hard disk). When this happens, I cannot do anything but wait for the end of the writing (which can take dozens of minutes) or perform a force shutdown. To avoid this situation, I would prefer to raise an exception when the array I am trying to allocate is too large for the RAM. How to do this?

OS: Windows 7 64 bits

Edit: I don't agree with the duplicate tag: I'm not asking about how to get the available memory, I'm asking about built-ins mechanisms that prevent me to allocate objects in memory without paging.

ndou
  • 1,048
  • 10
  • 15

1 Answers1

1

You could use psutil and periodically check psutil.virtual_memory() or similar.

a p
  • 3,098
  • 2
  • 24
  • 46