Consider the following loop:
    for batch in dataloader:
        batch = batch.cuda()
        features = model(batch)

If forwarding a batch takes up almost the memory in my GPU (let’s say 7gb out of 8), then this loop will fail on the second iteration due to a OOM error.
This version won’t:
    for batch in dataloader:
        batch = batch.cuda()
        features = model(batch)
        del features

Even though features is only a very small tensor (a dozen values). What does del exactly do here? Why do I need to manually free the memory to achieve what I want? Is this good practice? If no, what is an alternative that doesn’t require to halve the batch size?