none
Large string and memory leak RRS feed

  • Question

  • 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

    Thursday, March 15, 2012 2:22 AM

Answers

  • 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!

    • Marked as answer by Frank _ Chen Thursday, March 15, 2012 5:56 AM
    Thursday, March 15, 2012 4:35 AM
  • 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.

    Thursday, March 15, 2012 4:30 AM

All replies

  • 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.

    Thursday, March 15, 2012 4:30 AM
  • 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!

    • Marked as answer by Frank _ Chen Thursday, March 15, 2012 5:56 AM
    Thursday, March 15, 2012 4:35 AM
  • I see. Thank you very much!
    Thursday, March 15, 2012 5:56 AM