Large string and memory leak
-
2012年3月15日 上午 02:22
consider below case:
public class SimpleClass{
public string content;
}
var sc = new SimpleClass();
using(var f = new StreamReader("D:\\largtxt.txt"))
{
sc.content = f.ReadToEnd();
}
//do something with sc
I want to confirm, if the text file is large for exampe(50M), then the size of conent string maybe about 50M
then can the string of sc.content be GC? because I heard about that if a class is larger than 20K, then that class will be allocated in large object heap, and the larg object heap will never be GC? is that true?
Thanks
所有回覆
-
2012年3月15日 上午 04:30
Any object that is more than 85K qualifies for the large object heap. The only special handling for LOH is that it is allocated directly into Generation 2. But then it is very much in the managed heap which is managed by the Garbage Collector.
So whenever your Garbage Collector collects Generation 2 objects, the LOH objects also depending on whether they are still referenced or not, would also be collected.
- 已提議為解答 Sezhiyan Thiagarajan 2012年3月15日 上午 04:30
- 已標示為解答 Frank _ Chen 2012年3月15日 上午 05:56
-
2012年3月15日 上午 04:35
It's not like that. If you create a large object it is allocated in Generation 2. Since, it is likely that Generation 2 objects are going to be used for longer duration, Garbage Collector doesn't Generation 2 objects frequently. But, if you manually call GC.Collect() or if CLR determines Generation 2 must be collected in order to meet memory requirements, the Generation 2 objects are also collected.
In your case if sc.Content is large then it is ofcourse stay in Generation 2 but once sc object goes out of scope, the memory occupied by sc.Content is definitely be freed at some point of time. And you can also call GC.Collect once you finished with sc object.
Please mark this post as answer if it solved your problem. Happy Programming!
- 已標示為解答 Frank _ Chen 2012年3月15日 上午 05:56
-
2012年3月15日 上午 05:56I see. Thank you very much!

