4

I have a python programme which sometimes consumes a lot of RAM. I tried to restrict the usage by
resource.setrlimit(resource.RLIMIT_AS, (2**32, 2**32))
However, this causes Python to crash instead of raising a MemoryError.

How do I keep Python alive? I.e. I want the MemoryError.

The problem appears, when I try to solve a large instance in cvxpy. (This also means I cannot reduce memory usage of the code - it is not my code.) But when I allocate large amounts of RAM via

then i get the MemoryError as expected.

What is the difference?

Edit: The error message is:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

Edit: I now have a (M)WE.

import cvxpy as cvx
import numpy as np
import random
import resource
resource.setrlimit(resource.RLIMIT_AS, (2**32, 2**32))

# Problem data.
m = 500
n = 500
np.random.seed(1)
random.seed(1)
A = np.random.randn(m, n)
b = np.random.randn(m, 1)

# Construct the problem.
X = cvx.Semidef(m)
objective = cvx.Minimize(cvx.sum_entries(cvx.norm(X)))
constraints = [sum([X[random.randint(0,499),random.randint(0,499)] for _ in range(50)]) >= random.random() for _ in range(5000)]
prob = cvx.Problem(objective, constraints)

print("Optimal value", prob.solve(solver = 'SCS'))

After a few seconds it starts taking up large amounts of memory (more than 1GB, which is supposed to be the limit) and then crashes with the above error.

Hennich
  • 682
  • 3
  • 18

1 Answers1

3

Python only throws a MemoryError if it can figure out the error is going to happen before it actually happens. Otherwise, it will crash. Check out the official description of the MemoryError:

https://docs.python.org/2/library/exceptions.html#exceptions.MemoryError

Fardin Abdi
  • 1,284
  • 15
  • 20