[英]High memory usage of a windows forms method
I have ac# windows forms application which uses too much memory. 我有一个使用过多内存的ac#Windows窗体应用程序。 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.
我尝试调用GC /我知道这不是一个好主意/,但没有帮助。
Thank you in advance 先感谢您
You have not provided any info on which serializer you used in your program but I am quite inclined to think it is XMLSerializer
because it is prone to memory leak and you said in your comment that the program is crashing after working more then 10-12 hours. 您尚未提供有关在程序中使用哪个序列化程序的任何信息,但我很倾向于认为它是
XMLSerializer
因为它容易发生内存泄漏,并且您在评论中说,该程序在工作超过10-12小时后崩溃。
XmlSerializer
uses assembly generation, and assemblies cannot be collected. XmlSerializer
使用程序集生成,并且无法收集程序集。 As far as I know It does some caching for re-use but only for simple cases. 据我所知,它会做一些缓存以供重用,但仅适用于简单情况。
So if you have code like the following,which is called pretty often→ 因此,如果您有类似以下的代码,这通常被称为→
XmlSerializer xml = new XmlSerializer(typeof(MyObject), ....
Then you will get out of memory exception sooner or later. 然后,您迟早会遇到内存不足异常。
Edit: How to avoid Memory Leak from XMLSerializer: Please have a look at the Topic Dynamically Generated Assemblies in the following link→ MSDN Link If I just summarize what is written in there is that, you have a couple of ways. 编辑:如何避免XMLSerializer的内存泄漏:请在下面的链接→ MSDN链接中查看主题动态生成的程序集 。如果我只是总结一下其中写的内容,那么您有两种方法。
1) You can use the following constructors to avoid dynamic assembly 1)您可以使用以下构造函数来避免动态组装
XmlSerializer.XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)
2) Using a dictionary or hashtable, create your own caching 2)使用字典或哈希表,创建自己的缓存
private Dictionary<Tuple<Type, XmlRootAttribute>, XmlSerializer> cacheSerializer = new Dictionary<Tuple<Type, XmlRootAttribute>, XmlSerializer>();
public XmlSerializer GetXmlSerializer(Type type, XmlRootAttribute root) {
var key = Tuple.Create(type, root);
XmlSerializer xmlSerializer;
if (cacheSerializer.TryGetValue(key, out xmlSerializer)) {
return xmlSerializer;
}
xmlSerializer = new XmlSerializer(type, root);
cacheSerializer.Add(key,xmlSerializer);
return xmlSerializer;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.