I’m trying this, but it does not release GPU mem after each _collate_fn2 call:

def collate_fn2(batch,dev_id,extractor,lock):
    imgs, targets=default_collate(batch) # in CPU
    input1=[ imgs[i:(i+mini_mini_batch_size)].clone()
          for i in range(0, len(imgs) , mini_mini_batch_size) ]
    out = []
    lock.acquire()
    _collate_fn2(input1, out, dev_id, extractor)
    out = torch.cat(out, 0)
    input1 = None
    lock.release()
    return out, targets

def _collate_fn2(input1,out,dev_id,extractor):
    print('working')
    # input1 = [mini-minibatch1, mini-minibatch2, ..., mini-minibatch4]
    for x in input1:
        x = Variable(x, volatile=True)
        x = x.cuda(dev_id)
        x = extractor(x)
        out.append(x.cpu().data)
    print('done!')

class Collator(object):
    def __init__(self,dev_id,extractor,lock):
        self.dev_id = dev_id
        self.extractor = extractor
        self.lock = lock

    def __call__(self, batch):
        return collate_fn2(batch,self.dev_id,self.extractor,self.lock)

The log basically goes like this:

working
done!
working
THCudaCheck FAIL file=/py/conda-bld/pytorch_1493433037384/work/torch/lib/THC/generic/THCStorage.cu line=66 error=2 : out of memory

Is there anyway to focus releasing the memory?