The following code to create a global variable to store the contents of a text file txt word.txt, and then create a monitor to monitor the changes in word.txt, if the content changes word.txt reset txt variable. After running the program, if I word.txt content filling 100MB, memory usage of natural increase, but then I would word.txt content to just one character, the memory footprint is not down, but will increase the number of less .
public class Program
{
public static string txt = "aaa";
static void Main()
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(@"D:\ConsoleApplication1", "word.txt");
fileSystemWatcher.Changed += new FileSystemEventHandler(ReloadHandler);
fileSystemWatcher.NotifyFilter = NotifyFilters.Size;
fileSystemWatcher.EnableRaisingEvents = true;
Console.ReadLine();
}
private static void ReloadHandler(object sender, FileSystemEventArgs e)
{
ReloadTxtContent();
}
private static void ReloadTxtContent()
{
txt = null;
using ( FileStream fs = new FileStream(@"D:\ConsoleApplication1\word.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite) )
{
using ( StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default) )
{
txt = sr.ReadToEnd();
}
}
}
}
Is the global static variable assignment will repeatedly increasing memory usage? Has been cut down?