I'm trying to find a way to limit the memory available for the Python VM, as the option "-Xmx" in the Java VM does. (The idea is to be able to play with the MemoryError exception)
I'm not sure this option exist but there may be a solution using a command of the OS to "isolate" a process and its memory.
Thank you.
On *nix you can play around with the ulimit
command, specifically the -m (max memory size) and -v (virtual memory).
On Linux I'm using the resource module:
import resource
resource.setrlimit(resource.RLIMIT_AS, (megs * 1048576L, -1L))
Don't waste any time on this.
Instead, if you want to play with MemoryError
exceptions create a design that isolates object construction so you can test it.
Instead of this
for i in range(1000000000000000000000):
try:
y = AnotherClass()
except MemoryError:
# the thing we wanted to test
Consider this.
for i in range(1000000000000000000000):
try:
y = makeAnotherClass()
except MemoryError:
# the thing we wanted to test
This requires one tiny addition to your design.
class AnotherClass( object ):
def __init__( self, *args, **kw ):
blah blah blah
def makeAnotherClass( *args, **kw ):
return AnotherClass( *args, **kw )
The extra function -- in the long run -- proves to be a good design pattern. It's a Factory, and you often need something like it.
You can then replace this makeAnotherClass
with something like this.
class Maker( object ):
def __init__( self, count= 12 ):
self.count= count
def __call__( self, *args, **kw ):
if self.count == 0:
raise MemoryError
self.count -= 1
return AnotherClass( *args, **kw )
makeAnotherClass= Maker( count=12 )
This version will raise an exception without you having to limit memory in any obscure, unsupportable, complex or magical way.