Limit Python VM memory

前端 未结 3 1910
鱼传尺愫
鱼传尺愫 2020-12-03 10:57

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 e

相关标签:
3条回答
  • 2020-12-03 11:08

    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.

    0 讨论(0)
  • 2020-12-03 11:14

    On Linux I'm using the resource module:

    import resource
    resource.setrlimit(resource.RLIMIT_AS, (megs * 1048576L, -1L))
    
    0 讨论(0)
  • 2020-12-03 11:21

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

    0 讨论(0)
提交回复
热议问题