不确定是否是内存泄漏(ARC) [英] Not sure if it's a memory leak (ARC)

查看:95
本文介绍了不确定是否是内存泄漏(ARC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自定义动画,我怀疑内存泄漏,但不确定.每次运行给定的动画内存时,内存都会增加一点,但不会减少.为了确保这一点,我进行了测试:

I'm writing custom animations and I suspect that I have a memory leak, but I'm not sure. Every time I run a given animation memory goes up a little, but it doesn't go down. To make sure, I made a test:

NSLog(@"%@", self.weakanim);

// The animation collection to run
HyAnimationCollection * collection = [[HyAnimationCollection alloc] init];

self.weakanim = collection;

首先记录nil,然后始终记录一个地址.因此,这里有两个指标:

First this logs nil then it always logs an address. So there are two indicators here:

  1. 内存从9.7MB开始,每运行10次动画就会增加0.1MB.我测试了大约12MB.现在,是否应该每次都释放内存,或者仅仅是ARC(例如JVM的垃圾收集器)仅定期释放内存?也就是说,也许这不是泄漏,但是ARC尚未发布它,因为我还没有达到一定数量.
  2. 我将weakanim声明为weak只是为了查看以前的动画集合是否已发布,但是仍然出现相同的问题:ARC是否尚未发布 ?
  1. Memory starts at 9.7MB and goes up 0.1MB every 10 runs of the animation. I tested this to about 12MB. Now, should the memory be freed every time or is it just that ARC (like, say, JVM's garbage collector) only releases memory periodically? That is, maybe it isn't a leak, but instead ARC has not released it yet because I didn't reach a certain amount.
  2. I declared weakanim as weak just to see if the previous animation collection was being released, but the same issue still arises: is ARC not releasing yet?

推荐答案

  1. ARC不能像JVM一样工作.当您使用自动释放池"时,得到的结果最接近.在这种情况下,直到运行循环结束,对象才相关.如果您看到动画正在运行,则运行循环很可能正在运行,应该定期刷新池.

  1. ARC doesn't work like a JVM. The closest you get is when it uses the "autorelease pool." In this case, objects won't be related until the end of the run loop. If you can see the animation running then the run loop is most likely running and the pool should be flushed periodically.

一种更好的显示是否存在泄漏的方法是在您的HyAnimationCollection类的dealloc方法中放置一个断点.如果您的其余调查是正确的,我想它永远不会被调用.您可能在那个代码中有一个保留周期.

A better way of showing whether there's a leak would be to put a breakpoint in the dealloc method of your HyAnimationCollection class. If the rest of your investigation is correct, my guess is that it's never called. You probably have a retain cycle in that code.

这篇关于不确定是否是内存泄漏(ARC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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