Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management in C#

Tags:

c#

Good afternoon,

I have some text files containing a list of (2-gram, count) pairs collected by analysing a corpus of newspaper articles which I need to load into memory when I start a given application I am developing. To store those pairs, I am using a structure like the following one:

private static Dictionary<String, Int64>[] ListaDigramas = new Dictionary<String, Int64>[27];

The ideia of having an array of dictionaries is due to efficiency questions, since I read somewhere that a long dictionary has a negative impact on performance. That said, every 2-gram goes into the dictionary that corresponds to it's first character's ASCII code minus 97 (or 26 if the first character is not a character in the range from 'a' to 'z').

When I load the (2-gram, count) pairs into memory, the application takes an overall 800Mb of RAM, and stays like this until I use a program called Memory Cleaner to free up memory. After this, the memory taken by the program goes down to the range 7Mb-100Mb, without losing functionality (I think).

Is there any way I can free up memory this way but without using an external application? I tried to use GC.Collect() but it doesn't work in this case.

Thank you very much.

like image 673
Miguel Avatar asked Dec 06 '22 01:12

Miguel


1 Answers

You are using a static field so chances are once it is loaded it never gets garbage collected, so unless you call the .Clear() method on this dictionary it probably won't be subject to garbage collection.

like image 151
Darin Dimitrov Avatar answered Dec 20 '22 00:12

Darin Dimitrov