[Tutor] Memory Error

Steven D'Aprano steve at pearwood.info
Wed Feb 15 18:13:11 EST 2017


On Wed, Feb 15, 2017 at 09:36:02PM +0330, elham khanchebemehr wrote:
> Hi,
> I'm trying to write a code in python that takes an array of integers as
> sources, an array of integers as sinks, and an array of an array of
> integers of capacities, returning the maximum flow. I'm new to python and I
> got memory error, can you plaese help me because i have no idea how to
> solve this problem?

Start by showing us the error you are getting. COPY AND PASTE the entire 
traceback you get, starting with the line "Traceback..." to the end 
showing the error message, don't take a screen shot, don't retype it 
from memory, don't simplify it or summarise it.

Generally you get a memory error because you've run out of memory for 
the program. How much memory do you have on your computer? What OS is it 
running? 32-bit or 64-bit?

How big are entrances, exits and path?

If you are dealing with extremely large data sets, the overhead of 
Python's "boxed" data types can become significant. For example, on my 
computer a list of 100 zeroes takes 432 bytes:

py> import sys
py> data = [0]*100
py> sys.getsizeof(data)
432

If this is the problem, you may be able to reduce memory consumption by 
using the array module:

py> import array
py> data = array.array('b', [0])*100
py> sys.getsizeof(data)
132

Using the third-party numpy library may also help.

But for really large amounts of data, you can't avoid needing large 
amounts of memory.


Another comment, this is not related to your memory error, but this 
bit of code is terribly clunky:


>     for i in entrances:
>         augpathgen = bfs_paths(path, i, exits)
>         while True:
>             try:
>                 augpath = next(augpathgen)
>             except StopIteration:
>                 break
>             flows = [path[augpath[j]][augpath[j+1]] for j in range(len(augpath)-1)]


Replace that with:

    for i in entrances:
        augpathgen = bfs_paths(path, i, exits)
        for augpath in augpathgen:
            flows = [path[augpath[j]][augpath[j+1]] for j in range(len(augpath)-1)]
            ...



-- 
Steve


More information about the Tutor mailing list