Network module memory is not released in C++ libtorch

ko0starx: <p>I tested the example code in this issue <a href="https://github.com/pytorch/pytorch/issues/106255" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/issues/106255</a></p>
<p>I fixed forward as below</p>
<pre><code class="lang-auto">torch::Tensor forward(torch::Tensor x)
    {
        x = fc-&gt;forward(x);
        return x;
    }
</code></pre>
<p>Here is the result</p>
<pre><code class="lang-auto">Memory used: 382
Memory used: 766
Memory used: 1362
Memory used: 1362
Memory used: 978
</code></pre>
<p>Unlike the example code, Even after clearing the cache, some memory remains.</p>
<p>I just tried</p>
<pre><code class="lang-auto">cudaFree(input.data_ptr());
cudaFree(output.data_ptr());
</code></pre>
<p>result is</p>
<pre><code class="lang-auto">Memory used: 214
</code></pre>
<p>Still remains something Where is this from and how can I release this memory?</p>
<p>(Full code)</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;c10/cuda/CUDACachingAllocator.h&gt;
#include &lt;iostream&gt;

using namespace std;

class Net : public torch::nn::Module
{
public:
    Net(int fc_size = 100)
    {
        fc = torch::nn::Linear(fc_size, fc_size);
        fc-&gt;to(torch::kCUDA);
        register_module("fc", fc);
    }

    torch::Tensor forward(torch::Tensor x)
    {
        x = fc-&gt;forward(x);
        return x;
    }

private:
    torch::nn::Linear fc = nullptr;
    torch::Tensor x_copy;
};

double get_cuda_memory_usage()
{
    size_t free_mem, total_mem;
    cudaMemGetInfo(&amp;free_mem, &amp;total_mem);
    size_t used_mem = total_mem - free_mem;

    double used_mem_mb = used_mem / (1024.0 * 1024.0);
    return used_mem_mb;
}

void print_cuda_memory_diff(double start_mem, double end_mem)
{
    cout &lt;&lt; "Memory used: " &lt;&lt; end_mem - start_mem &lt;&lt; endl;
}

void memory_test(int size, double start_memory)
{
    // Create an empty tensor
    auto input = torch::empty({size, size}).to(torch::kCUDA);
    // GPU Memory: 1 * (input)
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());

    // Create a network with same size of tensor
    Net *net = new Net(size);
    // GPU Memory: 2 * (input)
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());

    // Pass the input through the network (cloning the input)
    auto output = net-&gt;forward(input);
    // GPU Memory: 3 * (input)
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());

    delete net;
    // GPU Memory: 3 * (input) (no change) (due to cache)
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());

    // Manually clear the cache and return all unused GPU memory (just for this example) (may not be recommended)
    c10::cuda::CUDACachingAllocator::emptyCache();
    // GPU Memory: 1 * (input)
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());

    cudaFree(input.data_ptr());
    cudaFree(output.data_ptr());
    print_cuda_memory_diff(start_memory, get_cuda_memory_usage());
}

int main()
{
    auto size = 10000;
    auto start_memory = get_cuda_memory_usage();

    memory_test(size, start_memory);

    return 0;
}
</code></pre>