tags:

views:

343

answers:

2

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.

+2  A: 

On *nix you can play around with the ulimit command, specifically the -m (max memory size) and -v (virtual memory).

mhawke
Thank you ! Exactly what I needed.
tweksteen
+1  A: 

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.

S.Lott
hum, funny answer :) Definitely more complicated than the first one and intrusive in my code. Oh, and thanks for teaching us what the factory design pattern is.
tweksteen
In your examples above, you might want to use xrange instead of range (assuming this isn't Python 3), otherwise you might run out of memory for the wrong reason.
Ned Deily
@tweksteen: if you think it's "intrusive" then you haven't done proper test-driven design. Also, if you're not using factories, you're not allowing for expansion of your class hierarchies. You should -- seriously -- rethink your design so that you use factories.
S.Lott
@Ned Deily: What is the chance that I wasn't serious about that silly-looking number?
S.Lott
@S.Lott: about 1 in 1000000000000000000000 ??
Ned Deily