Python shelve; unexpected RAM usage when loading/saving
I've been starting to use Python's shelve module (and I'm new-ish to Python per se), and while it is storing and loading information perfectly, the RAM usage is... strange.

The parts I want to save is a map that's on certain coordinates of another map, and a selection of values like 'fov1' and 'fov2' which may or may not exist for any given coordinate. For instance, on "minimap" at x=50 and y=100, there may be another map, and I want that saving. Minimap[50][100] may also have, for instance, a list for fov0 and fov1, but not anything else. The max fov value is fov75.

Here are my save/load functions. 

This save function is meant see which maps need saving (those which are not blank, and not currently saved), then to save the map as Xcoord + 'x' + Ycoord (eg '50x100') and save the fov parts as Xcoord + 'x' + Ycoord + 'f' + the number of the fov, (eg '50x100f2'). That all works fine. I also then want it to delete the map that's being held in RAM at that point, and to delete all the fov segments that exist so that the memory can be freed up. That also appears to work.

    def save_maps():
    file = shelve.open(savename, 'w')
    for x in range(200):
        for y in range(200):
            if minimap[x][y].map is not 'blank' and minimap[x][y].map is not 'saved':
                mapname = str(x) + 'x' + str(y)
                file[mapname] = minimap[x][y].map
                minimap[x][y].map = 'saved'
                for p in range(76):
                    try:
                        fov = getattr(minimap[x][y], 'fov' + str(p))
                        fovname = str(x) + 'x' + str(y) + 'f' + str(p)
                        file[fovname] = fov
                        delattr(minimap[x][y], 'fov' + str(p))
                    except:
                        pass
    file.close()

Meanwhile, here's the load function, meant to bring up the map, all the fovs, and put them back in. Again, this works perfectly.

    def load_map(x, y):
    file = shelve.open(savename, 'w')
    mapname = str(x) + 'x' + str(y)
    minimap[x][y].map = file[mapname] 
    for p in range(76):
        try:
            fovname = str(x) + 'x' + str(y) + 'f' + str(p)
            setattr(minimap[x][y], 'fov' + str(p), file[fovname])
        except:
            pass
    file.close()

However, the weirdness is with RAM usage, as I've been watching task manager as I test it. Simply saving a single map actually INCREASES the RAM python is using! Surely that can't be right? Alternatively, if I save a large chunk at once, the RAM goes down as it should, but then if I load them all back up, the RAM is then significantly higher than it was before they were ever saved. If I then save/load again, the RAM doesn't go up; it only increases on the first save/load cycle, then never again. I've been working on this all day and cannot work out what is causing this strangeness, and how it can be resolved. Can anyone help me out?

Edit: oh, yes, using Python 2.7.2. 
Python shelve; unexpected RAM usage when loading/saving
===

I've been starting to use Python's shelve module (and I'm new-ish to Python per se), and while it is storing and loading information perfectly, the RAM usage is... strange.

The parts I want to save is a map that's on certain coordinates of another map, and a selection of values like 'fov1' and 'fov2' which may or may not exist for any given coordinate. For instance, on "minimap" at x=50 and y=100, there may be another map, and I want that saving. Minimap[50][100] may also have, for instance, a list for fov0 and fov1, but not anything else. The max fov value is fov75.

Here are my save/load functions. 

This save function is meant see which maps need saving (those which are not blank, and not currently saved), then to save the map as Xcoord + 'x' + Ycoord (eg '50x100') and save the fov parts as Xcoord + 'x' + Ycoord + 'f' + the number of the fov, (eg '50x100f2'). That all works fine. I also then want it to delete the map that's being held in RAM at that point, and to delete all the fov segments that exist so that the memory can be freed up. That also appears to work.

    def save_maps():
    file = shelve.open(savename, 'w')
    for x in range(200):
        for y in range(200):
            if minimap[x][y].map is not 'blank' and minimap[x][y].map is not 'saved':
                mapname = str(x) + 'x' + str(y)
                file[mapname] = minimap[x][y].map
                minimap[x][y].map = 'saved'
                for p in range(76):
                    try:
                        fov = getattr(minimap[x][y], 'fov' + str(p))
                        fovname = str(x) + 'x' + str(y) + 'f' + str(p)
                        file[fovname] = fov
                        delattr(minimap[x][y], 'fov' + str(p))
                    except:
                        pass
    file.close()

Meanwhile, here's the load function, meant to bring up the map, all the fovs, and put them back in. Again, this works perfectly.

    def load_map(x, y):
    file = shelve.open(savename, 'w')
    mapname = str(x) + 'x' + str(y)
    minimap[x][y].map = file[mapname] 
    for p in range(76):
        try:
            fovname = str(x) + 'x' + str(y) + 'f' + str(p)
            setattr(minimap[x][y], 'fov' + str(p), file[fovname])
        except:
            pass
    file.close()

However, the weirdness is with RAM usage, as I've been watching task manager as I test it. Simply saving a single map actually INCREASES the RAM python is using! Surely that can't be right? Alternatively, if I save a large chunk at once, the RAM goes down as it should, but then if I load them all back up, the RAM is then significantly higher than it was before they were ever saved. If I then save/load again, the RAM doesn't go up; it only increases on the first save/load cycle, then never again. I've been working on this all day and cannot work out what is causing this strangeness, and how it can be resolved. Can anyone help me out?

Edit: oh, yes, using Python 2.7.2. 