可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've create a c# application which uses up 150mb of memory (private bytes), mainly due to a big dictionary:
Dictionary<string, int> Txns = new Dictionary<string, int>();
I was wondering how to free this memory up. I've tried this:
Txns = null;
GC.Collect();
But it doesn't seem to make much of a dent in my private bytes - they drop from say 155mb to 145mb.
Any clues?
Thanks
-edit-
Okay I'm having more luck with this code (it gets the private bytes down to 50mb), but why?
Txns.Clear(); // <- makes all the difference
Txns = null;
GC.Collect();
-edit-
Okay for everyone who says 'don't use GC.collect', fair enough (I'm not going to debate that, other than saying you can see my C background coming through), but it doesn't really answer my question:
Why does the garbage collector only free the memory if i clear the transaction list first? Shouldn't it free the memory anyway, since the dictionary has been dereferenced?
回答1:
Private bytes reflect the process' memory usage. When objects are collected the associated memory segment may or may not be freed to the OS. The CLR manages memory at the OS level and since allocating and freeing memory isn't free there's no reason to free each piece of memory immediately as chances are that the application will probably request more memory later.
回答2:
if u call GC.Collect() it start to do its job , but it returns immediately, it doesnt block, so you dont see its affect, if you just call GC.WaitForPendingFinalizers() after that it will block your app until GC.Collect() finish its job
回答3:
Most probably you have a hidden reference to the dictionary somewhere else. Thus the dictionary is not collected, but if you Clear()
it, the contents get collected.
As others have already noted, forcing the GC is not recommended. This might lead to memory being pushed into higher "generations" which are not often collected, thereby wasting more memory than gained in the long run.
回答4:
Not sure from memory if Dictionary
has a Dispose()
or not, but it's bound to have a Clear()
. Call either of these before setting any references to null
.
Then, simply let the garbage collector do its work. It is almost never a good idea to call GC.Collect()
explicitly yourself and it might not even do what you want/need/expect and end up costing you performance. Static Code Analysis (=FxCop) doesn't warn you with Reliability rule CA2001 about this for nothing, you know? Simply do not do this unless you really know what you're doing. And even then don't do it. ;-)
Are you sure the dictionary is that huge? Isn't it just 10 Mb of memory and the rest is otherwise taken by your application? Question that might help you: Have you used a profiler yet to see where memory is actually consumed...?
回答5:
Edit:
To be fair, setting the reference to null does not free the memory, it assigns it's container to a different address, in this case null. According to MSDN, calling Clear()
, does this: "The Count property is set to 0, and references to other objects from elements of the collection are also released. The capacity remains unchanged."
...
You should never call the garbage collector. You are using managed objects with no native resources, trust the garbage collector to clean up after you.
Besides the size of your dictionary, you don't need to worry about memory, memory isn't your problem, it's garbage collectors problem.
Calling Clear()
will remove the references to any contained object inside, but the capacity remains unchanged.
On a technical note, collecting memory is expensive and a considerable time consuming operating. Reason being, not only does the GC handle heap memory and clean up your heap, it also sort of defrags the heap. It tries to move memory into contiguous blocks to speed up allocation for when some piece of code makes a large request.
p.s. How big is your dictionary that you use 155MB of memory?
回答6:
Do you need the memory back? The memory is available, it's just not being reclaimed. You should not clear the dictionary, hold a weak reference to it and let the runtime do its job.
If you want to see really deep into what's going on, check out the .NET Memory Profiler. That will give you visibility into exactly what's happening with your object, what generation it is, what memory is being used by what and on and on. :)
回答7:
Ok, I have a theory here...
Dictionary is a collection of KeyValuePair which is again a reference type.
Your dictionary contains these keyValuePairs. When you say:
Txns = null
It frees the reference 'Txns' from those KeyValuePair collection. But still the actual memory of 150 Mb is being referenced by those KeyValuePair and they are in scope, thus not ready for garbage collection.
But when you use the following:
Txns.Clear();
Txns = null;
GC.Collect();
Here, the clear method also frees the 150Mb of data from their respective KeyValuePair object references. Thus those objects were ready for garbage collection.
Its just a wild guess I am making here. Comments are welcome :)
回答8:
Windows has two memory availability events. I'd expect the CLR to respond to this. If there's plenty of memory available, the smart thing is to NOT run the garbage collector. So, to make sure that you are indeed observing bad CLR behavior, please repeat this test with another dummy application using a big heap of memory.
回答9:
It's not usually a good idea trying to force the GC. Do you really need to have the whole dictionary in memory?