C#Windows窗体中的应用程序内存问题 [英] Memory problem with application in C# Windows Forms

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

问题描述

我有一个使用C#编写的应用程序,该应用程序在不应该使用的时候会保留过多的内存.可执行文件约为100Kb,整个应用程序为数千行代码.
它是主要组件,具有一个计时器,该计时器负责创建事件(具有几个属性的类的实例)并将其发送到此 http://timeline.codeplex.com/.时间轴接受事件的方式是通过调用ResetEvents函数并传递事件列表.因为我有一个计时器,所以将其放在计时器的代码中.
像这样运行它,应用程序将达到300Mb的内存,为了避免崩溃,我只是将其结束.如果我从计时器中删除对ResetEvents的调用,则应用程序运行会更平稳,消耗60-70Mb.没有时间轴的应用程序的运行速度应为10-20Mb.没有任何图形或其他可能会使用更多的图形.我的猜测是时间表可能有问题.

I have an application in C# which reserves too much memory when it wasn't supposed to. The executable is ~100Kb and the whole application is a couple thousands lines of code.
It's main component, has a timer which is responsible of creating events (instances of a class with a couple of attributes) and sending them to this http://timeline.codeplex.com/. The way the timeline accepts events, is by calling a ResetEvents function and passing a list of events. Because I have a timer, I put that inside the timer's code.
Running it like this, the application goes up to 300Mb of memory and I just end it to avoid crashing. If I remove the call of ResetEvents from the timer, then the application runs more smoothly consuming 60-70Mb. The application without the timeline, should run at 10-20Mb. There are no graphics or anything that could possibly use more than that. My guess is that something might be wrong with the timeline.

这是代码的一部分:

List<TimelineEvent> events = new List<TimelineEvent>();

...

内部计时器

TimelineLibrary.TimelineEvent newevent = new TimelineLibrary.TimelineEvent();
...
newevent.StartDate = starttime;
newevent.EndDate = endtime;
newevent.Id = id;
newevent.Title = title;
newevent.Description = description;
newevent.Link = url;
newevent.EventColor = color;

events.Add(newevent);

timeline.ResetEvents(events);

...

此代码位于计时器内部.我只是创建一个TimelineEvent,将其添加到列表中并调用ResetEvents.删除最后一行,不会引起内存问题.

This code is inside the timer. I just create a TimelineEvent, add it to a list and call ResetEvents. Removing that last line, doesn't cause the memory problem.

推荐答案

由于很难在没有更多代码的情况下查看问题所在,因此我建议尝试使用某种内存分析器来定位以及何时分配内存.

Since it is very hard to see what your problem is without more code, I suggest trying some kind of memory profiler to locate where and when the memory gets allocated.

例如尝试 RedGates Memory Profiler ,他们有一个基于时间的试用版.
遵循此演练起床可以加快速度并了解一些寻找的内容和方式.

Try for example RedGates Memory Profiler, they have a time-based trial.
Follow this walk-through to get up to speed and learn a bit what to look for and how.

有关.NET内存分析器的更多选项,请参见此线程.

For more options regarding .NET memory profilers, see this thread.

祝你好运!

这篇关于C#Windows窗体中的应用程序内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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