使用Mac上的Instruments进行内存泄漏检查 [英] Memory leak checking using Instruments on Mac

查看:1473
本文介绍了使用Mac上的Instruments进行内存泄漏检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚拉我的头发,试图让仪器咳嗽我故意构造的内存泄漏。我的测试示例如下:

  class Leaker 
{
public:
char * _array;
Leaker()
{
_array = new char [1000];
}

〜Leaker()
{
}
};

void * leaker()
{
void * p = malloc(1000);
int * pa = new int [2000];
{
Leaker l;
Leaker * pl = new Leaker();
}
return p;
}

int main(int argc,char ** argv)
{
for(int i = 0; i <1000; ++ i){
leaker();
}
sleep(2); //需要让仪器轮询内存
return 0;
}

基本上乐器从未发现明显的泄漏。我是为什么,但后来发现泄漏配置面板中的泄漏面板下的秒之间的自动检测。我把它回来的低,它会去,这是1秒,并把睡眠(2)在我的代码,瞧!泄漏发现!



据我所知,泄漏是一个泄漏,无论是发生在应用程序30分钟还是30毫秒。在我的情况下,我剥离测试用例回到上面的代码,但我真正的应用程序是一个没有UI或任何东西的命令行应用程序,它运行非常快;肯定小于默认的10秒采样间隔。



确定,所以我可以在测试模式退出我的应用程序几秒钟,但真正想要的,是在退出时只需要有仪器快照记忆,然后在应用程序运行时随时随地做任何需要。



所以...问题是:在退出应用程序时,是否有方法可以使仪器快照记忆,而不考虑采样间隔?



干杯,



Shane

解决方案

仪器,在泄漏模式下可以真正强大的泄漏跟踪,发现它比命令行程序(特别是在短时间后退出)更偏向基于事件的GUI应用程序。以前是一个 CHUD API ,您可以通过编程方式控制方面的仪器,但上次我尝试它的框架不再作为SDK的一部分提供。也许其中一些现在替换为 Dtrace



此外,确保您是Xcode的最新版本,因为这一领域最近有一些改进,可能会更容易做你需要的。你也可以在退出之前保持短暂的延迟,但使它有条件的环境变量的存在,然后设置环境变量在仪器的应用程序的启动属性,以便运行外部仪器没有延迟。 p>

I've just been pulling my hair out trying to make Instruments cough up my deliberately constructed memory leaks. My test example looks like this:

class Leaker
{
public:
    char *_array;
    Leaker()
    {
        _array=new char[1000];
    }

    ~Leaker()
    {
    }
};

void *leaker()
{
    void *p=malloc(1000);
    int *pa=new int[2000];
    {
        Leaker l;
        Leaker *pl=new Leaker();
    }
    return p;
}

int main (int argc, char **argv)
{
    for (int i=0; i<1000; ++i) {
        leaker();
    }
    sleep(2); // Needed to give Instruments a chance to poll memory
    return 0;
}

Basically Instruments never found the obvious leaks. I was going nuts as to why, but then discovered "sec Between Auto Detections" in the "Leaks Configuration" panel under the Leaks panel. I dialed it back as low as it would go, which was 1 second, and placed the sleep(2) in in my code, and voila; leaks found!

As far as I'm concerned, a leak is a leak, regardless of whether it happens 30 minutes into an app or 30 milliseconds. In my case, I stripped the test case back to the above code, but my real application is a command-line application with no UI or anything and it runs very quickly; certainly less than the default 10 second sample interval.

Ok, so I can live with a couple of seconds upon exit of my app in instrumentation mode, but what I REALLY want, is to simply have Instruments snapshot memory on exit, then do whatever it needs over time while the app is running.

So... the question is: Is there a way to make Instruments snapshot memory on exit of an application, regardless of the sampling interval?

Cheers,

Shane

解决方案

Instruments, in Leaks mode can be really powerful for leak tracing, but I've found that it's more biased towards event-based GUI apps than command line programs (particularly those which exit after a short time). There used to be a CHUD API where you could programmatically control aspects of the instrumentation, but last time I tried it the frameworks were no longer provided as part of the SDK. Perhaps some of this is now replaced with Dtrace.

Also, ensure you're up to date with Xcode as there were some recent improvements in this area which might make it easier to do what you need. You could also keep the short delay before exit but make it conditional on the presence of an environment variable, and then set that environment variable in the Instruments launch properties for your app, so that running outside Instruments doesn't have the delay.

这篇关于使用Mac上的Instruments进行内存泄漏检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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