繁体   English   中英

死物吞噬了记忆

[英]Dead objects eating up memory

在我的应用程序中,我遇到了内存使用问题,其中Loh充满了价值1.5GB的死对象(如下所示)。 对象存储在Byte数组中。

在此处输入图片说明

我试图通过运行以下代码来减少内存使用量来清理它,但它似乎没有起作用。

private static void OnScavengeProfileCache(object data)
{
 // loop until the runtime is shutting down
while(HostingEnvironment.ShutdownReason == ApplicationShutdownReason.None)
{
    // NOTE: Right now we only do the scavenge when traffic is temporarily low,
    // to try to amortize the overhead of scavenging the cache into a low utilization period.
    // We also only scavenge if the process memory usage is very high.
    if (s_timerNoRequests.ElapsedMilliseconds >= 10000)
    {
        // We dont want to scavenge under lock to avoid slowing down requests,
        // so we get the list of keys under lock and then incrementally scan them
        IEnumerable<string> profileKeys = null;
        lock (s_profileCache)
        {
             profileKeys = s_profileCache.Keys.ToList();
        }


        ScavengeProfileCacheIncremental(profileKeys.GetEnumerator());
    }

    // wait for a bit
    Thread.Sleep(60 * 1000);
}
}

private static void ScavengeProfileCacheIncremental(IEnumerator<string> profileKeys)
{
if (s_thisProcess.PrivateMemorySize64 >= (200 * 1024 * 1024) ) // 3Gb at least
{
    int numProcessed = 0;
    while(profileKeys.MoveNext())
    {
        var key = profileKeys.Current;
        Profile profile = null;
        if (s_profileCache.TryGetValue(key, out profile))
        {
            // safely check/remove under lock, its fast but makes sure we dont blow away someone currently being addded
            lock (s_profileCache)
            {
                if (DateTime.UtcNow.Subtract(profile.CreateTime).TotalMinutes > 5)
                {
                    // can clear it out
                    s_profileCache.Remove(key);
                }
            }
        }

        if (++numProcessed >= 10)
        {
            // stop this scan and check memory again
            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
            GC.Collect(); 
            break;
        }
    }
}
}

关于如何清理此处的内存使用情况的任何提示?

尝试System.GC.Collect()强制运行垃圾收集器。 这应该清除掉死物

暂无
暂无

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

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