Is there anything similar to empty_cache for host memory?

I’m running some code in a loop. I expect the memory to refresh after each run in the loop but it doesn’t happen automatically and deleting the tensor variables followed by a call to the garbage collector doesn’t do anything either. So, is there any function similar to torch.cuda.empty_cache for RAM? Or something else I can do to give pytorch a fresh start for each run?

Could you describe what “refresh” the memory would mean in this context? I don’t see any value in reusing host memory (besides page locked memory maybe) so could you also explain your expectations?

By “refresh”, I meant that I expect Python’s garbage collector to deallocate intermediate buffers that are no longer required. For instance, shouldn’t the layer’s output tensor (if not used anywhere else) be deallocated once it goes out of context (like in the 2nd iteration of a loop)?

I don’t think the GC should directly deallocate objects as I guess Python authors have thought about a proper way to balance too aggressive freeing vs. too much memory usage.
If you want to force the GC to free objects directly, you might need to use the GC interface and play around with some options.
Note that I have never touched this interface as it was never creating an issue for me.

I’ve tried using GC already. My current code looks like this:

    for some loop:  # I loop over different configs
        layer = torch.nn.Conv2d(32, 64, k, stride=s, padding=p, dilation=d, bias=False, device=host_device, dtype=torch.float32)
        X = torch.rand(1, 32, sz, sz, requires_grad=True, dtype=torch.float32).to(memory_format=torch.channels_last).detach()
        cpu_start = time.time()
        tmp = layer(X)
        cpu_fwd_time = time.time() - cpu_start
        del layer, X, tmp

Memory usage in one iteration affects the second iteration