简体   繁体   English

服务器上的内存分配/ 100%堆gen2碎片

[英]Memory Allocation / 100% Heap gen2 Fragmentation on Server

I have a .net Windows Service Application which, when started allocates all the existing Memory on the server. 我有一个.net Windows服务应用程序,启动时会在服务器上分配所有现有内存。 After memory profiling I can see, that 90% off the allocated memory is free in the gen 2 Heaps. 经过内存分析后,我可以看到,第2代堆中分配的内存减少了90%。 Fragmentation is at 100% 碎片率为100%

Running the application via .exe on my workstation dos not produce the same thing. 在我的工作站上通过.exe运行应用程序不会产生相同的结果。 Memory allocation seams normal here. 内存分配在这里正常。

I've tried to force GC every 5 Seconds to see if there is a problem with the GC. 我尝试每5秒强制一次GC,以查看GC是否存在问题。 Same problem 同样的问题

My question 1. What could cause memory fragmentation in this case? 我的问题1.在这种情况下什么会导致内存碎片? 2. Is there a way to configure the windows service in a way that makes it behave like my desktop? 2.是否可以通过某种方式配置Windows服务,使其表现得像我的桌面? 3. How would I go and find out what causes the problem? 3.我将如何去找出导致问题的原因?

Thanks alot 非常感谢

The most important difference between a service and a desktop application is the user running the application. 服务和桌面应用程序之间最重要的区别是运行该应用程序的用户。 Check it from the "Log On" tab on the Services control panel. 从“服务”控制面板上的“登录”选项卡中进行检查。 The default is "Local System Account" which has probably less rights than the desktop user. 默认值为“本地系统帐户”,其权限可能少于桌面用户的权限。 Change it to your current user and check your application again. 将其更改为当前用户,然后再次检查您的应用程序。

All objects that are about to die and have a finalizer are put into a queue called "finalizer queue". 将要死亡的所有具有终结器的对象都放入一个称为“终结器队列”的队列中。 The GC runs the finalizers one-by-one, and if a finalizer is stuck in an infinite loop, the whole GC is blocked. GC一对一地运行终结器,如果终结器陷入无限循环,则整个GC都将被阻塞。 The most visible effect of this is, the never ending increase of memory usage. 最明显的效果是,内存使用量永无休止地增加。 You can easilt simulate it by adding a code like this. 您可以通过添加这样的代码轻松模拟它。

~MyClass
{
    while (true)
    {
    }
}

First, check all your finalizers. 首先,检查所有终结器。 Execution from the local system account may somehow cause an infinite loop somewhere. 从本地系统帐户执行可能会在某种程度上导致无限循环。

Perhaps you don't have any finalizers, then you should do some heuristics on it. 也许您没有任何终结器,那么您应该对此进行一些启发。 Also there is a utility called dotMemory (not free, but have some trial period) that traverses all your heap for objects not freed. 还有一个名为dotMemory的实用程序(不是免费的,但有一定的试用期),该实用程序遍历所有堆中未释放的对象。 It can help. 它可以提供帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM