I have created a sample C# console application, which reads a file data in byte array and converts the byte array to hex string. It requires huge memory and it does not frees the memory after the work is done, I am also nullifying the variables in use.
Here is the sample code:
string filename = @"F:\\AVSEQ09.DAT"; //file size is 32 MB
string hexData = null;
byte[] fileDataContent = File.ReadAllBytes(filename);
if (fileDataContent != null)
hexData = BitConverter.ToString(fileDataContent);
fileDataContent = null;
hexData = null;
//GC.Collect();
Console.ReadKey();
If I run this code it takes 433 MB of private working set and if I uncomment the GC.collect call the memmory comes down to 6 MB. Why do I have to call GC.collect explicitly, is it bad to call GC.collect explicitly, how can I free the memmory (to 6 MB) without calling GC.collect?