简体   繁体   English

低内存崩溃iPhone

[英]low memory crash iphone

My iPhone application crashes due to low memory. 我的iPhone应用程序由于内存不足而崩溃。 I can see that a crashlog called lowmemory is saved after the crash. 我可以看到崩溃后保存了一个名为lowmemory的崩溃日志。 However, there are no leaks in the code. 但是,代码中没有泄漏。

Is there any other way to reduce the memory usage? 还有其他减少内存使用量的方法吗? I can see when profiling the app in simulator at one point the memory usage (allocations) reaches upto 91 mb and I suspect this is the point where the app is crashing on the device (iPhone 3GS). 我可以看到在模拟器上对应用程序进行性能分析时,内存使用量(分配)高达91 mb,我怀疑这是应用程序在设备(iPhone 3GS)上崩溃的地步。 At that time I am doing a lot of string replacements (basically I am replacing around 100 tags in HTML). 当时我正在做很多字符串替换(基本上是在HTML中替换大约100个标签)。

First of all I don't think that your app uses up to 91mb (is too huge). 首先,我认为您的应用程序占用的内存不超过91mb(太大)。 I think 91mb - is the size of memory that was allocated (and can be deallocated but this number wasn't decreased). 我认为91mb-是分配的内存大小(可以释放,但是这个数目没有减少)。 Could you check the Live Bytes column? 您可以检查“实时字节数”列吗? About string replacements: I don't know how you use such functionality. 关于字符串替换:我不知道您如何使用此类功能。 But I advice you to avoid to use NSString in this case and use NSMutableString and change string values inside - this allows you to avoid every time allocation of NSString objects. 但是我建议您避免在这种情况下使用NSString并使用NSMutableString并在内部更改字符串值-这样可以避免每次分配NSString对象。

You should try to use the Instruments tool. 您应该尝试使用乐器工具。 You can start the Instruments tool with XCode (cmd + I), and without XCode (cmd + space => Instruments). 您可以使用XCode(cmd + I)启动仪器工具,而无需使用XCode(cmd + space => Instruments)启动仪器工具。

If you start the Instruments with XCode, I suggest to use the "Leaks" option. 如果您使用XCode启动仪器,建议您使用“泄漏”选项。 You should press the "Leaks", select instead of "Statistics" the "Call Tree", and - in my experience is good to - check the following checkboxes: Hide Obj-C, Flatten recursion, Hide missing symbols, hide system libraries. 您应该按“泄漏”,而不是“统计”选择“调用树”,并且-以我的经验,很好-选中以下复选框:隐藏Obj-C,展平递归,隐藏缺少的符号,隐藏系统库。 Here the tool shows you the lines, which were appealed during the run. 在这里,该工具向您显示了在运行期间吸引的线条。

Another way is to start the Instruments without XCode. 另一种方法是在没有XCode的情况下启动仪器。 You should use here the "Activity Monitor", select your device and record it's activity. 您应在此处使用“活动监视器”,选择设备并记录其活动。 You should double click on one of the statistics to get the statistics list. 您应该双击统计之一以获取统计列表。 Find your app's name, and look at the "Real Memory" column. 找到您应用的名称,然后查看“ Real Memory”列。 Play a little with your app, and if the memory is increasing and not deallocating, you should know which classes were used for the operation you done, and you should check your classes. 试一下您的应用程序,如果内存在增加并且没有分配,您应该知道哪些类用于完成的操作,并且应该检查您的类。

Note: If you start the Instruments without XCode, be sure, the app isn't started from XCode in debug mode. 注意:如果您在不使用XCode的情况下启动仪器,请确保未在调试模式下从XCode启动该应用程序。 There was a bug in XCode 4.0 (I don't knew if later it was corrected or not), but it happened to me, that the Activity Monitor's Real Memory column had not shown the correct information. XCode 4.0中存在一个错误(我不知道以后是否可以更正),但是发生在我身上的是Activity Monitor的Real Memory列未显示正确的信息。

Note2: if you are making memory management testings, you should do it always with a device. 注意2:如果要进行内存管理测试,则应始终在设备上进行。 Always. 总是。

Edit: You can even try the "cmd + shift + B" shortcut key in XCode in your project, it suggests some possible memory leaks. 编辑:您甚至可以在项目的XCode中尝试“ cmd + shift + B”快捷键,这表明可能存在内存泄漏。 It is useful sometimes, but the tool hasn't every time right :) 有时它很有用,但是该工具并非每次都正确:)

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

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