Hello There:
Test code as following ,when the “loop” function return to “test” function , the GPU memory was still occupied by python , I found this issue by check “nvidia-smi -l 1” , what I expected is :Pytorch clear GPU memory when “loop” function return , so the GPU resource can be used by other programme.
How to clear GPU memory after “loop” function return ? Thanks in advance for any help you can provide!
from torchvision import models
import time
import gc
import torch

def loop():

    with torch.no_grad():
        model = models.resnet34()
        model=model.cuda()
    del model
    model=None
    gc.collect()
    time.sleep(3)
    torch.cuda.empty_cache()

def test():
    for i in range(10):
        time.sleep(5)
        print(f"the {i} loop")
        loop()

if __name__ == '__main__':
    test()