How to delete an object or a model entirely from memory in Keras/Python3?

Viewed 1371

Firstly, I would like to give the memory information before the processes.

pmem(rss=288796672, vms=4105973760, shared=107503616, text=2039808, lib=0, data=771235840, dirty=0)

I built a model using Keras and assign it to the model variable. Then, I sent the model object to a class constructor for cloning:

from tensorflow.python.keras.models import clone_model
from tensorflow.python.keras import backend as K
class Source:
    def __init__(self, model):
        config = Config()
        self.model = clone_model(model)
        # breakpoint to read memory
        self.model.compile(optimizer=config.optimizer, loss=config.loss, metrics=config.metrics)

The memory information after the cloning process is showed below:

pmem(rss=289615872, vms=4333002752, shared=107843584, text=2039808, lib=0, data=797331456, dirty=0)

So far, so good. When I try to delete the self.model object by del self.model, memory is not decreased.

pmem(rss=289615872, vms=4333002752, shared=107843584, text=2039808, lib=0, data=797331456, dirty=0)

Then I tried to execute garbage collector by gc.collect(), but the result was the same, nothing has changed.

pmem(rss=289615872, vms=4333002752, shared=107843584, text=2039808, lib=0, data=797331456, dirty=0)

Lastly, I tried to clear the session by using K.clear_session(). Nothing has changed again.

pmem(rss=289615872, vms=4333002752, shared=107843584, text=2039808, lib=0, data=797331456, dirty=0)

Keras version: 2.1.6 (downgraded from last version to solve this problem but it is not worked.)

Tensorflow version: 2.0.0-alpha0

1 Answers

TF never releases memory that it grabbed before. That's probably a rather good thing because it helps avoid mem fragmentation.

Note that this is independent of which part of this allocated mem is available for use - that TF controls it does not mean the mem is actively used. clear_session should make most/all of the controlled mem available again (but again: only to TF, not other processes).

You'll need to find out what part of the controlled mem is actually available, used, or needed.

Related