Reputation: 10247
We had an image conversion script running on .NET 4.0, IIS 7, ASP.NET, 4 GB server RAM that resizes large images and thus needs a lot of memory.
The first script increased memory usage to almost 100%, leaving virtually nothing for the SQL Server that was also running (which gave up memory until running on 20 MB instead the usual 900 MB).
In the second script we added a GC.Collect() and (to be sure) a one sec thread sleep after each cycle, and everything went back to normal.
Question: isn't that a flaw in the .NET memory management? Shouldn't the system take a closer look at what's happening with the available memory, slow things down and clean up?
Upvotes: 3
Views: 756
Reputation: 11
Yes garbage collection does its job when memory is not enough for the next operation. But.
Objects which are declared global or being used by global objects are unaffected by GC. So try to keep your objects local if possible.
Upvotes: 1
Reputation: 4744
It might be a flaw in the .Net memory management. We would need your code to be sure.
But I would first look into your code for cases where disposable resources are not disposed properly. This is known to cause memory leaks, and is the reason why the IDisposable interface exists.
Upvotes: 0
Reputation: 30922
According to the docs:
Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory.
I assume this situation hasn't been satisfied as SQL Server is backing down instead. As for it being a bug; the docs would suggest this is by design.
Upvotes: 4