[UWP]如何让垃圾收集器在正确的时间运行? [英] [UWP] How to make the garbage collector to run at the right time?

查看:64
本文介绍了[UWP]如何让垃圾收集器在正确的时间运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  情况就是这样。如果我启动我的应用程序并使其运行一小时,它将逐渐占用越来越多的非托管(本机)内存,并且在我明确调用GC.Collect()之前不会释放它。 GC.GC.WaitForPendingFinalizers();所以GC.Collect();
如果我不这样做,框架最终将关闭应用程序,但它不会调用垃圾收集器。这是我不明白的。为什么?页面上唯一活跃的东西是Microsoft Ad,但它看起来不像该组件中的内存泄漏或
任何其他组件。从内存管理环境(C#.NET)使用本机组件看起来是一个问题。可能管理的内存不会以非常慢的速度消耗或消耗,但同时与托管的
对象相关的非托管内存以更高的速率消耗。因此,垃圾收集器逻辑不会开始收集,因为它不知道本机端的严重内存消耗。正如我所提到的,如果我调用GC.Collect(); GC.GC.WaitForPendingFinalizers();所以GC.Collect();
内存释放到应用程序刚启动时的水平。在这种情况下我该怎么办?我应该在一定时间间隔收集电话吗?在这种情况下,良好的间隔是多少?或者是否可以将垃圾收集器调整为
更好地适应这种情况?

   Here is the situation. If I start my application and leave it running for an hour it will gradually take more and more unmanaged (native) memory and won't free it until I explicitly call GC.Collect(); GC.GC.WaitForPendingFinalizers(); GC.Collect(); If I don't do it the framework will eventually close the application but it won't call the garbage collector. This is what I don't understand. Why? The only active thing on the page is Microsoft Ad but it doesn't look like a memory leak in that component or any other components. It looks like an issue on using native components from memory managed environment (C# .NET). Probably the managed memory is not consumed or consumed at very slow rate but at the same time the unmanaged memory somehow related to the managed objects is consumed at much higher rate. So, the garbage collector logic does not start collection because it does not know about serious memory consumption on the native side. As I mentioned if I call GC.Collect(); GC.GC.WaitForPendingFinalizers(); GC.Collect(); the memory is freed up to the level it was when the application was just started. What should I do in this case? Should I call collection at certain intervals? What would be the good interval in this case? Or is it possible tune up the garbage collector to work better for this scenario?

谢谢

Alex

推荐答案

在没有看到您的代码的情况下,除了一般建议之外,很难给您提供更多信息。  确保在完成后尽可能地丢弃所有内容。  使用积木是你的朋友。如果要在代码中添加事件处理程序,请确保在完成对象时删除
,否则将阻止对象被垃圾回收。
Without seeing your code it is hard to give you anything more than general advice.  Make sure you dispose of everything you can when you are done with it.  Using blocks are your friend. If you are adding event handlers in code make sure you remove them when you are done with the object or it will prevent the object from being garbage collected.


这篇关于[UWP]如何让垃圾收集器在正确的时间运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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