Reputation: 1148
I have a program that collects large amounts of data in memory and, after analyzing, discards it quickly as well. The problem is that the memory consumption is getting high. I would like to set the garbage collector to collect more frequently to reduce the amount of memory. Is that possible without directly calling gc.collect()
(which is bad practice)?
Upvotes: 1
Views: 936
Reputation: 89396
I have a program that collects large amounts of data in memory and, after analyzing, discards it quickly as well
GC.Collect() was put there for a reason, and this is it. Sometimes you just know so much more about the state of the managed heap that you can pick the perfect time for an extra garbage collection.
Upvotes: 1
Reputation: 804
You rather need to ensure that you have not got any handles to the data. These can include:
The delegates are especially quite cryptic because you don't realise that they are blocking the data.
Upvotes: 0
Reputation: 13864
If you're frequently gathering, using, and then disposing of data then in order to avoid the memory consumption climbing up and up only for the application/system to stall for a GC pause then you need to avoid new allocations at all costs.
To that end, you should avoid immutable datatypes. Instead of storing your raw data in classes/lists/etc see if you can store data in things like byte[] arrays which can be emptied out and re-used without requiring a new allocation. You'd then wrap your arrays up in readers which parse and make sense of the data as you go along.
In short, the new
keyword is your worst enemy in this situation.
Upvotes: 0