Windows窗体方法的高内存使用率 [英] High memory usage of a windows forms method

查看:101
本文介绍了Windows窗体方法的高内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用过多内存的C#Windows窗体应用程序.代码的安全性,就是问题

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);
                            }
                        }
                    }
                }
            }

这会导致一个巨大的问题,并且应用程序会因内存不足"而崩溃.有人可以给我建议如何减少其内存使用量.我尝试调用GC/我知道这不是一个好主意/,但没有帮助.

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.

提前谢谢

推荐答案

您尚未提供有关程序中使用的序列化程序的任何信息,但我很倾向于认为它是XMLSerializer,因为它容易发生内存泄漏然后您在评论中说,该程序在工作10至12个小时以上后便崩溃了.

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使用程序集生成,并且无法收集程序集.据我所知,它会做一些缓存以供重用,但仅适用于简单的情况.

XmlSerializer uses assembly generation, and assemblies cannot be collected. 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.

如何避免XMLSerializer的内存泄漏: 请看一下主题 在以下链接中动态生成的程序集

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.

1)您可以使用以下构造函数来避免动态组装

1) You can use the following constructors to avoid dynamic assembly

XmlSerializer.XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)

2)使用字典或哈希表,创建自己的缓存

2) Using a dictionary or hashtable, create your own caching

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;
}

这篇关于Windows窗体方法的高内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆