3
votes

I have a program and in it, it creates tabs, and every time I create one the memory usage goes up, but when I close a tab out the memory doesn't go down. I am just wondering, is this normal, and is there something I can do about it? Here is my code for closing out tabs:

def removeTab(self, index):

  text[index].deleteLater()
  del text[index] # text box

  # reorganize dict
  last = -1
  for key in sorted(text):
    if last+1 != key:
      text[key-1] = text[key]
      del text[key]
    last += 1

  self.tab_widget.setCurrentIndex(index)
  widget = self.tab_widget.currentWidget()
  self.tab_widget.removeTab(index)  # remove tab
  widget.deleteLater()
  del widget
4
I posted this on Google Plus because I would love to see this answered. - Devyn Collier Johnson
This sounds like it has to do with garbage collection; en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 - RobinJ

4 Answers

2
votes

To put it crudely:

  1. The amount of memory available to a python application can grow during its lifetime, but it can never fully shrink back to where it started.

  2. There is no way to force python to release unused memory back to the system.

So the best you can do is try to manage the amount by which the memory grows. One obvious way to do this is to delete unneeded objects, so that newly created objects can re-use the memory that has already been allocated.

But what if the application seems unable to re-use the memory? This either means that objects are not being deleted cleanly (i.e. there are references to them being kept somewhere), or, much less likely, that there is an underlying memory leak in PyQt/Qt.

Tracking down extra references kept to objects can sometimes be quite tricky (to say the least). Some common "hiding places" in PyQt are signals connected using lambda (or functools.partial), or monkey-patched protected methods. But there are no hard-and-fast rules for finding them.

As it stands, the example code in the question does not seem to have any places where extra references could be "hiding". So if the memory usage goes up "every time" a tab is created (as the OP claims), then the problems must lie elsewhere in the application - or to be more precise, the problems probably lie in how the objects are created, rather than in how they are deleted.

1
votes

You are using python. "Del" only removes the object reference, it does no deallocate memory. It's the garbage collector who is responsible for memory deallocation (and afaik, you can't control it).

The same will happend if you keep a reference to the object somewhere else.

0
votes

Okay, someone on Google Plus gave a suggestion.

Kevin Kennedy (https://plus.google.com/u/0/105980580368438890195/posts) suggestions

".close() before .deleteLater()".

0
votes

In general memory allocated by malloc or new is not returned to the system when freed/deleted. Instead it is retained by the application and reused for later allocations. If you need to return the memory then you should look at using a custom allocator.