Adding collectgarbage in trainable depending in the memory size instead of iterations