Python Memory usage doesnt drop

烂漫一生 提交于 2019-12-11 06:09:44

问题


I am writing a twisted PB application that seems to use a very large amount of memory that never seems to get released when the user disconnects.

I have a pb.Root object that clients connect to and invoke a remote method that returns a pb.Referenceable object this object reads a large amount of information into memory (about 2GBs worth of data) when it is created to speed up the actions. This object, along with some other information about the client is inserted into a list.

When the client disconnects from the server, I invoke some clean up actions on this object to remove references to the cache object that is being stored. The chunkCache is the dict that i am storing the data in.

def disconnected(self):
    self.connected = False
    self.chunkCache = None
    self.cur.close()

Once the Client has disconnected the memory usage according to top never drops it still says 2Gb.

Should I be worried about this or will the allocated memory get released when its needed, or if not any ideas how I can release this memory? It is created when the object is created and not passed anywhere else.

Inside that object I do have one deferToThread call, could this be stopping the item being released?

I'm using python 2.7 on Linux.

UPDATE:

Im confused, I have just added custom __del__ methods to my object and put a print statement in there and they are being deleted, so why does the memory usage never drop?

Thanks

Dean


回答1:


On most current OSes, allocation of dynamic memory happens on what is often called the heap (not to be confused with the data structure of the same name), which is essentially just a contiguous region that starts at some base address and extends to the base address plus the current size of the heap. Processes start out with a small heap - typically just a few pages - and it grows as needed by extending the segment upwards in units of positive integer multiples of the system page size. Within that space, the allocation/deallocation create smaller chunks of memory to satisfy the needs of the program, along with whatever metadata the allocator needs to track what memory is allocated and what isn't. These smaller pieces of memory may be deallocated over time, leaving the heap segment essentially empty, but unless it's specifically coded, the heap segment is rarely if ever shrunk. As a result, a long-running process will tend to appear to have a heap segment that is as large as it's peak usage has been. The pages that were in use but aren't any more will tend to be moved off to swap if physical memory needs to be freed up for other processes, but the process image still appears to be "large", and generally won't shrink without being restarted. There are ways around that using other memory allocation mechanisms (e.g. mapping a temporary file for memory space and unmapping and removing it when done), but the program needs to be specifically coded to do so. Applications written in C or C++ that use the standard library allocation routines (malloc/free or new/delete, respectively), including the Python compiler/interpreter, will tend to exhibit the above behavior.



来源:https://stackoverflow.com/questions/20999853/python-memory-usage-doesnt-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!