调试内存泄漏VS2015。大量本地堆 [英] Debuging memory leak VS2015. Massive native heap

查看:2955
本文介绍了调试内存泄漏VS2015。大量本地堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决内存问题,我绝对可以看到,但我不知道发生的时间和地点。



我的托管堆大小似乎很好(100MB),但是本机堆大小在未知时刻开始增长,直到达到〜2GB,应用程序正在崩溃。



我的应用程序正在运行许多线程,它在许多循环中通过EF 6进行了大量的Db连接。
这就是为什么我通过寻找日志或者插入点来调试代码真的很困难。



我想也许我可以看看是什么问题在内存中,只有我可以看到,我的本机堆大小主要由大小为8,192字节的对象填充。所以我可以看到这个问题真的发生了,但仍然没有任何线索。



我不知道我是否使用了100%的Visual Studio内存分析器功能。 >
现在我可以看到:



还有什么可以做的找到问题?



也许这是愚蠢的问题,但我正在处理这个问题两天,几乎达到了我的想法限制。



我已经通过Break点,日志,代码分析,但我仍然没有任何线索。



我将不胜感激。



15:11 2017/02/03



我能够找到负责泄漏的代码,但对我来说仍然没有意义。这个代码有可能造成大量内存泄漏吗?



代码是:

 code> public class DbData:IDisposable 
{
private DBEntity db;

public DbData()
{
db = new FruitDBEntity();
}

public Fruit AddFruitDefinition(Fruit fruit)
{
lock(thisLock)
{
var newFruit = db.Fruits.Where (f => f.FruitId == fruit.FruitId)
.Where(f => f.FruitName == fruit.FruitName)
.Where(f => f.FruitColor == fruit.FruitColor)
.FirstOrDefault();
if(newFruit == null)
{
newFruit = db.Fruits.Add(fruit);
db.SaveChanges();
}
return newFruit;
}
}
}

DbData 是每次想使用方法 AddFruitDefinition()时创建的:

  using(var data = new DbData())
{
data.AddFruitDefinition();
}


解决方案

两个快照。
只要我看到(从图像),你只需要一个快照。



该怎么办?


  1. 使用分析器启动应用程序。


  2. 执行常规步骤并拍摄快照。


  3. 重复步骤2中所做的相同步骤,并采取另一个快照。

  4. 停止应用程序。您应该看到2个快照,点击2ns快照,然后选择与#Snapshot 1进行比较。
    可能需要一些时间来处理结果。

  5. 您应该可以看到几个附加报告中的列(标识符,计数,大小,模块,计数差异大小差异
    最后两列很重要,它们告诉哪个类第二次使用更多/更少的内存。 >

总结一下,你需要弄清楚内存泄漏在哪里,并解决它,你会通过比较快照来做到这一点。




I am struggling with memory issue which I can definitely see but I don't know where and when exactly it's happening.

My managed heap size seems to be ok (100MB), but native heap size is starting to growing in unknown moment and it's still going until it will reach ~2GB and app is crashing.

My application is running many threads, and it's doing a lot of Db connection through EF 6 in many loops. That's why it's really hard for me to debug code just by looking to logs or putting break points.

I thought maybe I can see what's the issue by looking at memory but what only I can see it's that my native heap size is mostly filled by objects with size of 8,192 bytes. So I can see that problem is really happening but still have no clue why.

I am not sure if I am using 100% capabilities of Visual Studio memory profiler.
What I can see now is:

What else or more I can do to find the issue?

Maybe it's silly question but I am working on this problem for two days and I've almost reached my ideas limit.

I've went through Break points, logs, code analyze but I am still without any clue.

I will be grateful for any idea.

[EDIT] 15:11 2017/02/03

I was able to find code responsible for the leak, but it still has no sense for me. How it's possible that this code is causing massive memory leak?

The code is :

public class DbData : IDisposable
{
    private DBEntity db;

    public DbData()
    {
        db = new FruitDBEntity();
    }

    public Fruit AddFruitDefinition(Fruit fruit)
    {
        lock (thisLock)
        {
            var newFruit = db.Fruits.Where(f => f.FruitId     == fruit.FruitId)
                                    .Where(f => f.FruitName   == fruit.FruitName)
                                    .Where(f => f.FruitColor  == fruit.FruitColor)
                                    .FirstOrDefault();
            if (newFruit == null)
            {
                newFruit = db.Fruits.Add(fruit);
                db.SaveChanges();
            }
            return newFruit;
        }
    }
}

Class DbData is created every time I want to use method AddFruitDefinition():

using ( var data = new DbData() ) 
{
    data.AddFruitDefinition();
}

解决方案

First, you need at least two snapshots. As far as I can see (from image) you took only one snapshot.

What to do?

  1. Start application with profiler.

  2. Do usual steps and take snapshot.

  3. Repeat same steps which you did in step 2, and take another snapshot.
  4. Stop application. You should see 2 snapshots, click on 2ns snapshot and select Compare to #Snapshot 1. It might take some time to process results.
  5. You should be able to see few additional columns in report (Identifier, count, Size, Module, Count Diff. Size Diff. Last two columns are important. They tell which class used more/less memory second time.

To sum up... you need to figure out where is memory leak and to fix it. And you'll do it by comparing snapshots.

这篇关于调试内存泄漏VS2015。大量本地堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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