I have a c# windows forms application which uses too much memory. The peace of code, that is the problem is this
private void mainTimer_Tick(object sender, EventArgs e)
{
try
{
if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
{
if (File.Exists(Globals.pathNotifFile + "1"))
{
File.Delete(Globals.pathNotifFile + "1");
File.Move(Globals.pathNotifFile, Globals.pathNotifFile + "1");
}
File.Move(Globals.pathNotifFile, Globals.pathNotifFile + "1");
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
{
return;
}
if (Globals.shotsFired != true)
{
CreateDLclient();
Globals.shotsFired = true;
}
if (Globals.pathNotifFile == null)
{
return;
}
var data = Deserialize();
foreach (var notifyData in data.@new)
{
if (notifyData.Status == "1" || notifyData.Status == string.Empty)
{
if (DateTime.Now >= Convert.ToDateTime(notifyData.DateTime))
{
if (notifyData.Message != string.Empty)
{
notifyData.Status = SendMessageToUser(notifyData.Message, notifyData.Company, notifyData.EmojiCode);
Serialize(data);
}
else
{
notifyData.Status = "3";
Serialize(data);
}
}
else if (DateTime.Now >= Convert.ToDateTime(notifyData.DateTime).AddMinutes(5))
{
if (notifyData.Message != string.Empty)
{
notifyData.Status = SendMessageToUser(notifyData.Message, notifyData.Company, notifyData.EmojiCode);
Serialize(data);
}
else
{
notifyData.Status = "3";
Serialize(data);
}
}
}
}
}
It causes a huge problem and the application crashes with 'out of memory' Can somebody give me an advice how can I reduce the memory usage of that. I've tried to invoke the GC /I know it is not a good idea/, but it didn't help.
Thank you in advance