[Tutor] Question about the memory manager

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Jan 11 11:02:09 EST 2016


On 11 January 2016 at 15:40, Peter Otten <__peter__ at web.de> wrote:
>> I can't even work out how you trigger a MemoryError on Linux (apart
>> from just raising one). I've tried a few ways to make the system run
>> out of memory and it just borks the system rather than raise any error
>> - I can only interrupt it with REISUB.
>>
>> Here's a simple one:
>>
>> $ python -c 'x = []; x.append(iter(x))'
>>
>> (Make sure you save all your work before trying that!)
>
> You can set the interpreter on a diet:
>
> $ ulimit -v 22000
> $ python -c 'print "x"'
> x
> $ python -c 'print "x"*10**6'
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
> MemoryError

This didn't initially work for me:

$ ulimit -v 22000
$ python -c 'print "x"*10**6'
python: error while loading shared libraries: libc.so.6: failed to map
segment from shared object: Cannot allocate memory

I guess that limit's too low so (in a new terminal):

$ ulimit -v 50000
$ python -c '"x"*10**8'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
MemoryError
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line
66, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1,
in <module>
    from apport.report import Report
  File "/usr/lib/python2.7/dist-packages/apport/report.py", line 20, in <module>
    import apport.fileutils
  File "/usr/lib/python2.7/dist-packages/apport/fileutils.py", line
22, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python2.7/dist-packages/apport/packaging_impl.py",
line 20, in <module>
    import apt
  File "/usr/lib/python2.7/dist-packages/apt/__init__.py", line 21, in <module>
    import apt_pkg
ImportError: /usr/lib/python2.7/dist-packages/apt_pkg.so: failed to
map segment from shared object: Cannot allocate memory

Original exception was:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
MemoryError

So it seems that it ran out of memory and sys.excepthook failed
because of insufficient memory which is the situation Alex Martelli
described. However:

$ python -c 'raise ValueError'
Traceback (most recent call last):
  ...
ImportError: /usr/lib/python2.7/dist-packages/apt_pkg.so: failed to
map segment from shared object: Cannot allocate memory

Original exception was:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError

It seems I need a ulimit of 60000 to get this to work properly:

$ ulimit -v 60000
$ python -c 'raise ValueError'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError
$ python -c '"x"*10**8'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
MemoryError

--
Oscar


More information about the Tutor mailing list