Memory leak while looping

Isaac To kkto at csis.hku.hk
Fri Apr 18 04:38:29 EDT 2003


>>>>> "Roger" == Roger Hancock <roger_hancock at hotmail.com> writes:

    Roger> Thanks for the help.  The xrange call solved my problem.
    Roger> However, I'm still a little puzzled by the behavior I'm seeing
    Roger> with the range call.  With my now better understanding of the
    Roger> range call, I would expect to see my function take a big chunk of
    Roger> memory until the function exited.  Instead, the function
    Roger> continues to grab more and more memory.  First, it takes most of
    Roger> my physical memory (256 MB) and then proceeds to eat up my swap
    Roger> memory.  Eventually my machine reboots.  Pretty impressive, huh?

In your original program, the range() call never terminate.  It tries to
grab a huge (100M integers with each 16-bytes would mean 1.6G bytes) memory
which is allocated in the VM, and then try to initialize every of the cell
by writing different numbers into them.  If your computer does not try to
limit the amount of memory allocated for each process, all the time will end
up used for swapping, and at the end the swap is filled and your program
will be terminated.  But at the time when the swap is filled, other program
might also fails, because they never expects a full swap space.  This might
explain why you see the computer reboots.

    Roger> Just imagine the damage I could do with 4 lines of code.

You don't need 4 lines.  You need just the expression range(1,100000000).
Nothing more.  You can try running Python and type that into the prompt to
see the behaviour.

If you don't like that, you can limit the amount of memory for each process.
Different OS has different way to do that, and I only know that for
Unix/Linux (which is the only OSes I use) I will use "ulimit -v 40960" to
limit all process to use no more than 40Mi bytes of virtual memory.  Then
instead of killing other programs, the OS just kill the program saying that
the memory limit has been exceeded.

Regards,
Isaac.




More information about the Python-list mailing list