iPhone/Instruments:“malloc”是什么?对象摘要中的条目?
我正在调整我的 iPhone/iPad 应用程序的性能,似乎并没有释放所有应该释放的内存。在仪器中,当我在模拟器中模拟内存警告后,还剩下很多“Malloc”条目;他们怎么样了?我可以摆脱它们吗?它们是什么意思/代表什么?
非常感谢,
Stefan
I'm performance tuning my iPhone/iPad app, it seems like not all the memory gets freed which should be. In instruments, after I simulate a memory warning in my simulator, there are lots of "Malloc" entries left; what's about them? Can I get rid of them, what do they mean/what do they stand for?
Thanks a lot,
Stefan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在任何时候,您的应用程序都会有(大量)活动对象,即使在收到内存警告(以及操作系统随后的内存恢复)之后也是如此。因此,您经常会看到许多这样的 malloc。
它们本身并不表示内存分配有问题,而可能只是表明您的程序正在运行。
另请参阅SO 主题,了解有关对象分配工具的更多信息。
此外,您可以使用许多高级技术来检测内存分配问题。
在这里您可以找到一个很棒的教程,它将让您超越 Leaks 工具所允许的范围。
编辑:
关于这些 malloc 的确切含义,您必须认为您可以分配两大类对象(粗略地说):通过 Obj-C 运行时系统创建的 Objective-C 对象和“普通”C对象,通过 malloc 分配。
第二类的许多对象是由系统库和编译器 C 库(例如套接字或文件句柄等)分配的(无需直接调用 malloc)。这些 (C) 对象没有与之关联的类型信息,因此 Instruments 只是向您显示分配的内存块的大小,而没有更多可用信息。
很多时候,malloc 对象是由更高级别的类创建的,因此当您恢复与其实例关联的内存时,通过 malloc 分配的内存也会被释放。
您不应该特别担心它们,除非您看到它们的总体大小随着程序执行“无限增长”。在这种情况下,您需要首先研究分配/释放更高级别对象的方式,并了解代码中的哪些地方陷入困境。
At any time, your app will have a (huge) number of living objects, even after getting a memory warning (and the subsequent memory recovery by the operating system). So, it is pretty common that you will also see many of those mallocs you are seeing.
They are not in themselves a sign that something is wrong with memory allocation, but possibly only of the fact that your program is running.
Also have a look at this S.O. topic to learn more about the object allocation tool.
Furthermore, there are many advanced techniques you can use to detect memory allocation problems.
Here you can find a great tutorial that will allow you to go way beyond what the Leaks tool allows you to.
EDIT:
About the exact meaning of those mallocs, you have to think that you can allocate two broad classes of objects (to put it roughly): Objective-C objects that are created through the Obj-C runtime system, and "normal" C objects, that are allocated through malloc.
Many object of the second class are allocated (without you directly calling malloc) by system libraries and by the compiler C library (think about, e.g., sockets or file handles, whatever). Those (C) objects do not have type information associated to them, so Instruments simply shows you the size of the allocated memory block, without having more information available.
Many times malloc objects are created by higher-level classes, so that when you recover memory associated to their instances, also memory allocated through malloc is freed.
You should not worry specifically about them, unless you see that their overall size "grows indefinitely" along program execution. In such case you need first to investigate the way you alloc/release your higher level objects and understand where in your code things get stuck.