Python MemoryError - how can I forcefully delete an object

I have a program that processes multiple files and a report is generated for each file. The part that generates the report is a separate function that takes a filename and then returns. During report generation, intermediate parts are cached in memory because they can be used across multiple parts of a report to avoid recalculation.

When I run this program on all files in a directory it will run for a while and then it crashes with a MemoryError. If I then re-run it in the same directory, it will skip all the files for which it has successfully generated a report and continues. It will process multiple files before crashing again.

Now why aren't all resources cleaned up or marked at least for garbage collection after calling the method that generates the report? You have no instances and I am not using any globals, and after every file processing, all open files are closed.

Are there ways to check if there are additional references to an object? Is there a way to force garbage collection in Python?

A little more detail on implementation and cache. Each report has several items in it, each item can then rely on different calculations, each calculation can depend on other calculations. If one calculation has already been done, I do not want to do it again (most of them are expensive).

Below is an abbreviated version of the cache:

class MathCache:
    def __init__(self): self.cache = {}
    def get(data_provider):
        if not data_provider.id in self.cache:
            self.cache[data_provider.id] = data_provider.get_value(self)
        return self.cache[data_provider.id]

      

An instance is created and then passed to each item in the report. This instance is only stored as a local reference in the report creation method.

All data_providers inherit from a generic class, which is used to create a unique identifier for an instance based on the hash constructor arguments and the class name. I am passing MathCache as the data_provider itself may rely on other calculations.

0


a source to share


1 answer


You should check out the gc module: http://docs.python.org/library/gc.html#module-gc .



+3


a source







All Articles