简体   繁体   English

长时间不活动后,.NET应用程序非常慢

[英].NET Application very slow after long period of inactivity

I'm not sure if the question title is the best one but it was the best one I could come up with... 我不确定问题标题是否是最好的,但它是我能想到的最好的...

I have this .NET (C#) applications which starts up with Windows and remains opened until the computer is turned off. 我有这个.NET(C#)应用程序,它启动Windows并保持打开,直到计算机关闭。 The app stays on the tray and I open it by clicking the tray icon and close it the same way. 应用程序停留在托盘上,然后单击托盘图标将其打开并以相同方式关闭它。

The app is not slow at first, it works normally, no problems there. 该应用程序一开始并不慢,它正常工作,没有问题。 But after long periods of inactivity of itself, it gets very slow when showing it again for the first time in a long period. 但是经过长时间的不活动,在很长一段时间内第一次再次显示它时会变得很慢。 Know what I mean. 明白我的意思了吗。

For instance, I could not use/open (click the tray icon) for a few days and between those days I opened and closed and used lots of other apps, heavy apps too and I probably hibernated and resumed the computer a few times and when I needed to open my app again, it was slow. 例如,我无法使用/打开(点击托盘图标)几天,在那些日子里我打开和关闭并使用了很多其他应用程序,也是重型应用程序,我可能已经休眠并重新启动计算机几次,当我需要再次打开我的应用程序,这很慢。 After a few minutes of using it, it goes back to normal and works fine. 使用几分钟后,它恢复正常并正常工作。

I believe this has something to with memory management and the system probably frees up most of my app's memory so other programs can use it more efficiently. 我相信这与内存管理有关,系统可能会释放我应用程序的大部分内存,因此其他程序可以更有效地使用它。 And maybe .NET memory management as something to do with it... 也许.NET内存管理与它有关...

Whatever the reason, is there anything I can do to optimize my app regarding that issue? 无论是什么原因,我可以做些什么来优化我的应用程序的问题?

This is almost certainly due to memory being paged out to disk. 这几乎可以肯定是由于内存被分页到磁盘。

When you cease using your application and other applications or tasks start exerting memory pressure, pages of memory from your app can be written out to disk. 当您停止使用您的应用程序和其他应用程序或任务开始施加内存压力时,应用程序中的内存页面可以写入磁盘。 When you try to use your application again, all this data must then be read in causing the stalls that you see. 当您再次尝试使用您的应用程序时,必须读入所有这些数据,从而导致您看到的停顿。

Unfortunately, there is no good solution - if you run as administrator or have the SeLockMemoryPrivilege, you can lock parts of your application into physical memory. 不幸的是,没有好的解决方案 - 如果您以管理员身份运行或拥有SeLockMemoryPrivilege,您可以将应用程序的某些部分锁定到物理内存中。 You can try "touching" pages periodically to keep them in physical memory. 您可以定期尝试“触摸”页面以将它们保存在物理内存中。 Unfortunately, both these options will cause your application to interact badly with other applications on the system - your memory is getting paged out because the physical memory is needed for something else. 不幸的是,这两个选项都会导致您的应用程序与系统上的其他应用程序进行严重交互 - 您的内存将被分页,因为其他内容需要物理内存。 You can attempt to lower your overall memory footprint, but you will still run into this issue in some cases. 您可以尝试降低总体内存占用量,但在某些情况下仍会遇到此问题。 There are options for tweaking your working set size, but the OS is free to ignore those, and will in many cases. 有一些选项可以调整你的工作集大小,但操作系统可以自由地忽略这些,并且在很多情况下也是如此。

You can use the .NET memory profiler to get a good idea of what your application is doing with memory over time. 您可以使用.NET内存分析器来了解应用程序随着时间的推移对内存执行的操作。 You can check if anything is building up where you don't expect it to (collections, lists, etc.) and causing your memory footprint to grow. 您可以检查是否有任何内容正在建立在您不期望的位置(集合,列表等)并导致您的内存占用增长。

Have you tried double-clicking in Process Explorer on your running app and observe it for leaks? 您是否尝试在正在运行的应用程序上双击Process Explorer并观察它是否泄漏? Is it CPU utilization, UI leak, memory leak, recurrent I/O? 是CPU利用率,UI泄漏,内存泄漏,重复I / O?

如果问题确实是由于长时间不活动而导致程序被分页,您是否尝试设置“保持活动”计时器,每隔几分钟触发一次,这基本上使应用程序处于寻呼系统的状态有没有看到它闲置?

I reckon that this behavior is normal in Windows. 我认为这种行为在Windows中是正常的。 Windows will serve the application that is need it most, ie, the application that is currently in used. Windows将提供最需要的应用程序,即当前使用的应用程序。 Thus, if an application is minimized or not active for pro-long duration, like the case specified above, it will be the best candidate for Windows to page the application memory and make available memory to active application. 因此,如果应用程序被最小化或长时间不活动(如上面指定的情况),它将是Windows 寻呼应用程序内存并为活动应用程序提供可用内存的最佳候选者。 Windows will restore the application memory when it get active from paging memory (hard disk), thus this explain why it is slow. Windows将从分页内存(硬盘)激活后恢复应用程序内存,这就解释了为什么它很慢。

This might be not the best answer, but if I were you, I will experiment by increasing the base priority of the application. 这可能不是最好的答案,但如果我是你,我将通过增加应用程序的基本优先级来进行实验。 This can be done by using Thread object in .NET framework. 这可以通过在.NET框架中使用Thread对象来完成。

Thanks. 谢谢。

This isn't a root cause answer or solution, but if Michael is right and there's nothing you can really do about it, then you might just want to consider visually informing the user that your application is active with a progress bar or nifty progress circle. 这不是根本原因的答案或解决方案,但如果迈克尔是正确的并且您无法真正做到这一点,那么您可能只是想通过进度条或漂亮的进度圈在视觉上通知用户您的应用程序处于活动状态。

It won't speed up your program, but it may ease the minds of your users just a little bit. 它不会加速你的程序,但它可以让你的用户的思想稍微放松一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一段不活动后的会话超时 - Session timeout after period of inactivity wcf服务应用程序后,asp.net应用程序工作非常缓慢 - asp.net application is working very slow after wcf service application Signalr-闲置一段时间后运行任务 - Signalr - Run task after period of inactivity C#WinForm应用程序UI长时间冻结 - C# WinForm application UI freezes after long period 为什么System.Net.Sockets.Socket.AcceptAsync在长时间不活动后与ConnectionReset一起完成? - Why does System.Net.Sockets.Socket.AcceptAsync complete with ConnectionReset after a long time of inactivity? 在具有Azure AD凭据的ASP .NET MVC中长时间不活动后,如何处理CryptographicException? - How to handle CryptographicException after long inactivity in ASP .NET MVC with Azure AD credentials? 在二楼公司探索 ASP.NET 应用程序非常缓慢 - Very slow exploring ASP.NET application at second floor company 一段时间不活动后,SignalR JavaScript集线器方法未在IE中触发 - SignalR javascript hub method not firing in IE after a period of inactivity 安装项目后,C#应用程序的运行速度非常慢 - C# application works very slow after Setup project 发布后的ASP.NET MVC页面非常慢 - ASP.NET MVC page is very slow after publishing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM