My free memory is MUCH LARGER than allocating memory and I still get out of memory error. [RuntimeError: CUDA out of memory]

C_J: <p>I have this weird issue,</p>
<p>RuntimeError: CUDA out of memory. Tried to allocate 3.62 GiB (GPU 0; 47.99 GiB total capacity; 24.39 GiB already allocated; 20.41 GiB free; 24.71 GiB reserved in total by PyTorch)</p>
<p>My free memory (20.41GiB) is LARGER than the memory that it tries to allocate (3.62GiB).<br>
Actually it’s 16.79GiB larger than what it needs.</p>
<p>I tried to find similar case and found one,</p><aside class="quote quote-modified" data-post="1" data-topic="114890">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/c6cbf5/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/cuda-out-of-memory-even-when-i-have-enough-free-solved/114890">Cuda Out of Memory, even when I have enough free [SOLVED]</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    EDIT: SOLVED - it was a number of workers problems, solved it by lowering them 
I am using a 24GB Titan RTX and I am using it for an image segmentation Unet with Pytorch, 
it is always throwing Cuda out of Memory at different batch sizes, plus I have more free memory than it states that I need, and by lowering batch sizes, it INCREASES the memory it tries to allocate which doesn’t make any sense. 
here is what I tried: 
Image size = 448, batch size = 8 

“RuntimeError: CUDA error: out of memory”
…
  </blockquote>
</aside>
<p>
In the post they say “it was a number of workers problems, solved it by lowering them”<br>
But I don’t think it applies to my case.</p>
<p>Can I get some help please?</p>
<p>Thanks for reading!</p>

JuanFMontesinos: <p>It’s not like it’s free. It’s reserved, which counts in practice as used. So you have 45 already compromised and want to allocate 4.</p>

C_J: <p>Thanks for reply JuanFMontesinos,<br>
In same code with slightly different setting (added 1 more gpu), I have this:</p>
<p>RuntimeError: CUDA out of memory. Tried to allocate 3.62 GiB (GPU 2; 47.99 GiB total capacity; 13.14 GiB already allocated; 31.59 GiB free; 13.53 GiB reserved in total by PyTorch)</p>
<p>In this case if we add 13.14+13.53=26.67GiB, we still have enough memory to allocate 3.62GiB.<br>
And it doesn’t match your explanation.</p>

JuanFMontesinos: <p>Can you verify via <code>nvidia-smi</code> command that you are not consuming resources with any other task before running this?</p>
<p>Also, how are you running this multi-gpu scenario? Note that modules such as Dataparallel usually have a main gpu which requires more memory.</p>

C_J: <p>I am monitoring using nvidia-smi, but the memory usage never reaches close to maximum.<br>
In some other post, I saw that someone is calculating free memory as:<br>
free memory = reserved memory - allocated memory<br>
In this case, it makes sense.<br>
But then I’m not sure how to reserve more memory, since it looks like it’s done automatically.</p>

learner12345: <p>I’m also getting a similar type of error.<br>
I have finetuned Yolov5 on custom dataset, it went very well, after getting best weights. I tried to refine those best weights by training for more 30 epochs. But I get cuda out of memory error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/e/deeae45bb034bae66c1834e163b3950635fde0ba.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/deeae45bb034bae66c1834e163b3950635fde0ba" title="WhatsApp Image 2022-04-21 at 2.02.34 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/deeae45bb034bae66c1834e163b3950635fde0ba_2_690x388.jpeg" alt="WhatsApp Image 2022-04-21 at 2.02.34 PM" data-base62-sha1="vO1fDLrB6uZQAgok6lTgJuoffku" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/deeae45bb034bae66c1834e163b3950635fde0ba_2_690x388.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/deeae45bb034bae66c1834e163b3950635fde0ba_2_1035x582.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/e/deeae45bb034bae66c1834e163b3950635fde0ba.jpeg 2x" data-dominant-color="8A8C8C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">WhatsApp Image 2022-04-21 at 2.02.34 PM</span><span class="informations">1280×720 141 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I tried to empty  cache memory as Suggested by stack overflow  but still can’t get out of this error. Althought memory is not being used by the GPU. I have checked using Nvidia-smi</p>
<p>Any help will be appreciated much,<br>
many thanks</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/why-did-pytorch-run-through-the-code-before-when-i-run-it-again-this-time-i-reminded-cuda-out-of-memory-tried-to-allocate-50-00-mib-gpu-0-6-00-gib-total-capacity-353-72-mib-already-allocated-3-86-gib-free-396-00-mib-reserved-in-total-by-pytorch/118287/3">here</a>.</p>

C_J: <p>It says you have only 252.69MiB Cuda memory and you are trying to allocate 250.00MiB, which is pretty close. This makes sense to me that you don’t have enough GPU memory available.<br>
In my case it tries to allocate 3.62GiB but there are 20.41GiB free GPU memory, which I think is weird.</p>