image748×270 6.83 KB

how to free the cpu memory
testcode:
import torch
from torchvision import models
import gc


@profile
def test():
    net = models.alexnet(pretrained=True)
    gpu = torch.device('cuda:0')
    net.to(gpu)
    net.cpu()
    del net
    aaa = [1,2,3,4]
    torch.cuda.empty_cache()
    bbb = [1,123,4,151]
    gc.collect()
    aa = 1
    
if __name__ is "__main__":
    print("main")
    test()

runcmd : python -m memory_profiler profile_try.py