3

In my application I need to load large files (can be about ~ 250 MB) into memory, I'm doing it in a lazy way - when user ask to see a file - I'm loading it. After that, every time user tries to access the file, I'm able to show it immediately , because it's already located in memory. So, The problem is with garbage collection... Every file I'm loading, I'm loading through a WeakReference, BUT : I tested It a few times, I was able to load about 3GB into memory (than app become not usable), but GC did not occurred. I'm not able to call GC.Collect(2), because I can not determine time to call it, so how to tell GC to collect memory (weakreferences) in good moments (damn, 3GB is too much...It seems GC just does not doing his job) Hot to resolve it? I really need lazy loading, but I need memory to be collected when process uses more than 1GB of it, or something like that

illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84

1 Answers1

1

There is a static function called GC.GetTotalMemory(bool forceFullCollection) ( http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx ). You can use it to force a Garbage Collection before loading a new file into the memory, if you have passed some threshold.

Edit: a possible implementation

public MyFile GetMyFile(){
   if ( !is_my_file_in_memory() ) {
      if (CG.GetTotalMemory(false) > MY_THRESHOLD ) {

        GC.Collect(2);

      }
      load_my_file_in_memory();
   }
   return get_my_file_from_memory();
}
linepogl
  • 9,147
  • 4
  • 34
  • 45
  • I mentioned, that I can not determine moment to force Garbage Collection...It's not a good idea to collect memory after next file was loaded, there will be absolutely no lazy load – illegal-immigrant Mar 01 '11 at 16:51
  • And I mentioned that you can use this function *before* loading lazily a file into the memory :-) – linepogl Mar 01 '11 at 16:55
  • GC.GetTotalMemory(true) before next file loading did not helped...I'm still able to load about 3GB into memory and then app freezes – illegal-immigrant Mar 01 '11 at 17:04
  • I have added a pseudo-implementation to make sure that we talk about the same thing. – linepogl Mar 01 '11 at 17:11
  • Yes, we are talking about same thing. I though about this solution too, but I don't want to create a limitation in my app (MY_THRESHOLD)...Actually, It seems that I found a root of my problem - some objects that obtain strong references from weakReference.Target, does not free up them! – illegal-immigrant Mar 01 '11 at 17:16