On Wed, 31 Aug 2011 10:33 pm Steven D'Aprano wrote: > Twice in a couple of weeks, I have locked up my PC by running a Python 2.5 > script that tries to create a list that is insanely too big. > > In the first case, I (stupidly) did something like: > > mylist = [0]*12345678901234 [...] > Apart from "Then don't do that!", is there anything I can do to prevent > this sort of thing in the future? Like instruct Python not to request more > memory than my PC has?
For anyone who may care, I can report that ulimit under Linux will help with this situation. [steve@wow-wow ~]$ ulimit -v 200000 [steve@wow-wow ~]$ python Python 2.5 (r25:51908, Nov 6 2007, 16:54:01) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> L = range(100000000) Traceback (most recent call last): File "<stdin>", line 1, in <module> MemoryError (Of course, I would have eventually got a MemoryError even without the ulimit. *Eventually.* After much thrashing and processes crashing and pain.) Does anyone else think it would be useful for Python's memory manager to enforce user-settable limits? Even just a simple thing like "never try to allocate more than N bytes at once" would probably go a long way to prevent a certain class of accidental (or deliberate) DOSes. -- Steven -- http://mail.python.org/mailman/listinfo/python-list