我有一個長時間運行的應用程序,由於內存泄漏一致失敗。是否引用靜態屬性導致內存泄漏
我懷疑我使用靜態屬性可能是原因。下面是我今天的例子:
public class StaticReferences
{
public static readonly object Fixed1 = new object();
}
public class ShortLived
{
public object Object1;
}
public class Doer // This class is instantiated once
{
public void DoStuff() // This method is called over and over again.
{
var shortLived = new ShortLived()
{
Object1 = StaticReferences.Fixed1
};
}
}
將(通過ShortLived.Object1
屬性)的ShortLived
其參考StaticReferences.Fixed1
實例得到妥善收集垃圾一旦超出範圍?
是什麼讓你覺得有內存泄漏? –
這是一個控制檯應用程序,它運行需要幾個小時完成的過程。它會因內存不足異常而失敗。我通過Jetbrains DotMemory運行它。非託管內存空間不斷增長。託管memorý是穩定的.. –
@JakobGade你的程序做什麼需要非託管內存?該部分是由您還是通過第三方組件實施的?非託管內存如何處理? – xxbbcc