Garbage collection runs too late - causes OutOfMemory exceptions

旧时模样 提交于 2019-12-05 18:05:34

It shouldn't really be possible for a Garbage Collection to 'not to happen in time'. They happen when a new memory allocation would push Gen-0 past a certain limit. Thus they always happen before a memory allocation would push the memory past its limit. This happens so many times a day throughout the world I would be surprised if any bugs weren't well known about.

Have you considered that you might actually be allocating more memory than is available? The OS only lets you access 2GB on most 32-bit machines.

There are some other possibilities:

  1. Is your application using un-managed memory?
  2. Is your application Pinning any memory? If so that could cause a fragmentation issue especially if you aren't releasing pin.
Thomas

If you use a lot of memory and you garbage collect a lot I guess you should consider the "Flyweight" design pattern.

As an example, if you garbage collect a lot of strings, see String.Intern(string s). Msdn reference

Jo9876

You can use GC.collect() to force the garbage collector to do its work. But it is not preferable.

Use memory profiles like(memprofiler) to detect the leaks. Almost all your code performs leaks at some points.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!