What is the disadvantage of using pin_memory?

gaoking132: <p>From the official pytorch document (<a href="http://pytorch.org/docs/notes/cuda.html#use-pinned-memory-buffers" rel="nofollow noopener">http://pytorch.org/docs/notes/cuda.html#use-pinned-memory-buffers</a>), it seems that by pinning your batch in cpu memory, the data transfer to GPU can be much faster.</p>
<p>Then comes the question: why by default pin_memory is False in DataLoader?  I tried to recall the minimal knowledge learned from operation system classes. Does pin-memory indicate that once a batch is pinned, it will always stay in the memory until the process ends?</p>

smth: <p>pinned memory is page-locked memory. It is easy for users to shoot themselves in the foot if they enable page-locked memory for everything, because it cant be pre-empted. That is why we did not make it default <code>True</code></p>

gaoking132: <p>Could you suggest scenarios when we should use pin_memory?</p>

smth: <p>try to use pin_memory, but if you are seeing system freeze or swap being used a lot, disable it.</p>

isalirezag: <p>can pin memory cause getting out of memory error for GPUs?</p>

jdhao: <p>I am seeing this error when using <code>pin_memory=True</code> in dataloader.</p>

imy: <p>Yeah, some  problem may cause by the <code>pin_memory</code></p>

ytwanghaoyu: <p>This post in 2017 solved my problem. It’s 2023 now but I still facing this problem caused by pin-memory, and it takes me a while to figure it out. I mean, the documentation really needs to specify the risk of using pin-memory instead of recommending people use it.</p>

ptrblck: <p>The <a href="https://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers">docs</a> already give an explicit warning as:</p>
<blockquote>
<p>This is an advanced tip. If you overuse pinned memory, it can cause serious problems when running low on RAM, and you should be aware that pinning is often an expensive operation.</p>
</blockquote>
<p>and also <code>pin_memory</code> is not set to <code>True</code> in e.g. the <code>DataLoader</code>. In case you have any suggestions where more warnings are needed, could you share them please?</p>

ytwanghaoyu: <p>Thanks for your reply and the warning info in the docs (which I don’t take it seriously at first).</p>
<p>But I still wonder in what situation pin_memory = True will become a problem, could you give some examples? It will be great if you would like to introduce some articles to give us some intuition about it.</p>

ptrblck: <p>Pinning memory disallows the system to use this memory for its allocations. A very simple example of an undesired behavior would be to pin too much memory, which would then force your OS into memory thrashing and moving pages into the swap thus tanking the performance of your entire system.</p>

ytwanghaoyu: <p>Thanks! That’s also what my situation is. I will find some articles about it to learn.</p>

dinarkino: <p>I’ve spent considerable amount of time for debugging DDP training on 4 GPUs. At some random point, training process slowed down by 3 times. Everything was getting very slow. And this stayed even after training finish. Only server restart was able to fix the slow-down. It was quite hard to find the real cause of the problem, we even thought on hardware problems, but in the end the whole thing is in one flag pin_memory=True</p>
<p>I would really recommend setting pin_memory to False in all cases. And then set it to True to check, if it adds any speedup.</p>