Pytorch do not clear GPU memory when return to another function

Hello ptrblck :

When the programme start , first run at

time.sleep(5)

GPU state as following ,only take 650MB

When in loop function ,GPU state as following ,take 1136MiB

When the loop function return to test function ,python still resident in GPU memory ,take 1028MiB
image

How to release GPU resource when loop function return , which means no python resident in GPU memory ? Than you.