App的Spiky Memory“健康”吗?

时间:2022-09-06 20:56:08

I've recently been developing an app that processes a large amount of data very frequently (~15 times a minute). To do so, I allocated a large chunk of memory, then freed it for each batch of data.

我最近开发的应用程序非常频繁地处理大量数据(每分钟约15次)。为此,我分配了一大块内存,然后为每批数据释放它。

Here's a screen of my Memory Allocations from Instruments: App的Spiky Memory“健康”吗?

这是我的仪器记忆分配屏幕:

The Memory usage oscillates from about 3MB to about 30MB pretty quickly. I was just wondering, is this "healthy," per se for the iPhone.

内存使用率很快就从大约3MB扩展到大约30MB。我只是想知道,iPhone本身是“健康的”。

Is it risky to allocate and free so much memory so quickly? Is it unsustainable, or just bad practice?

如此快速地分配和释放如此多的内存是否有风险?这是不可持续的,还是只是不好的做法?

Thanks!

3 个解决方案

#1


7  

It is neither risky nor necessarily bad practice. Allocating and freeing memory takes time, so doing it very frequently vs. doing it once and re-using the allocated memory is a trade-off between memory usage effiency (using the lowest amount of memory at every single moment) and performance.

这既不危险也不一定是坏习惯。分配和释放内存需要时间,因此非常频繁地执行此操作并重新使用分配的内存是在内存使用效率(使用每个时刻最低内存量)和性能之间进行权衡。

If the performance of your app doesn't suffer at the moment, you have probably made the correct choice regarding this tradeoff for your app.

如果您的应用程序的性能目前没有受到影响,您可能已经为您的应用程序做出了正确的选择。

Generally speaking, using 30 MB of memory is quite a large amount for older devices (iPhone 3G and older). You cannot be sure that your app has that much memory available so be prepared to received memory warnings. If your app cannot reduce its memory usage when it receives a memory warning, the OS might kill it.

一般来说,对于较旧的设备(iPhone 3G及更早版本),使用30 MB的内存是相当大的数量。您无法确定您的应用程序是否有足够的可用内存,因此请准备好接收内存警告。如果您的应用在收到内存警告时无法减少其内存使用量,操作系统可能会将其终止。

#2


4  

My primary worry in these situations would be fragmentation. If the chunks are all the same size though, you should be fine (and looking at your graph, the peaks appear to be completely level, so I think that's the case).

我在这些情况下的主要担忧是碎片化。如果块大小都相同,那么你应该没问题(看看你的图表,峰值似乎是完全水平的,所以我认为就是这种情况)。

You will be paying allocation costs, but as Ole says, if your app is performing well enough already, there's not much point in trying to optimize that.

您将支付分配成本,但正如Ole所说,如果您的应用程序已经表现良好,那么尝试优化它并没有多大意义。

#3


-1  

It depends, if the user has an iPhone 4 or iPhone 3GS it should be do-able but on the iPhone 3G it will result in a memory warning very quickly. iPhone 4 has 256mb of RAM for the apps ( 512 mb in total ) iPhone 3GS has 128mb for the apps, and 256 in total the iphone 3g only has 128mb and 64mb for the apps.. usually having around 40mb free when no apps are running.

这取决于,如果用户拥有iPhone 4或iPhone 3GS,它应该可以使用,但在iPhone 3G上它会很快产生内存警告。 iPhone 4为应用程序提供了256mb的RAM(总共512mb)iPhone 3GS的应用程序为128mb,总共256个iphone 3g只有128mb和64mb的应用程序..通常在没有应用程序运行时可以免费使用40mb 。

As apple says you should only allocate the memory you really need, and try to not use autorelease too much, because autorelease gives us an object being allocated while we don't really need it anymore

正如苹果所说,你应该只分配你真正需要的内存,并尝试不要过多地使用自动释放,因为autorelease给我们一个分配的对象,而我们不再需要它了

If the performance isn't too bad, I would try using less memory and allocating more when you really need it.

如果性能不是太差,我会尝试使用更少的内存并在您真正需要时分配更多内存。

#1


7  

It is neither risky nor necessarily bad practice. Allocating and freeing memory takes time, so doing it very frequently vs. doing it once and re-using the allocated memory is a trade-off between memory usage effiency (using the lowest amount of memory at every single moment) and performance.

这既不危险也不一定是坏习惯。分配和释放内存需要时间,因此非常频繁地执行此操作并重新使用分配的内存是在内存使用效率(使用每个时刻最低内存量)和性能之间进行权衡。

If the performance of your app doesn't suffer at the moment, you have probably made the correct choice regarding this tradeoff for your app.

如果您的应用程序的性能目前没有受到影响,您可能已经为您的应用程序做出了正确的选择。

Generally speaking, using 30 MB of memory is quite a large amount for older devices (iPhone 3G and older). You cannot be sure that your app has that much memory available so be prepared to received memory warnings. If your app cannot reduce its memory usage when it receives a memory warning, the OS might kill it.

一般来说,对于较旧的设备(iPhone 3G及更早版本),使用30 MB的内存是相当大的数量。您无法确定您的应用程序是否有足够的可用内存,因此请准备好接收内存警告。如果您的应用在收到内存警告时无法减少其内存使用量,操作系统可能会将其终止。

#2


4  

My primary worry in these situations would be fragmentation. If the chunks are all the same size though, you should be fine (and looking at your graph, the peaks appear to be completely level, so I think that's the case).

我在这些情况下的主要担忧是碎片化。如果块大小都相同,那么你应该没问题(看看你的图表,峰值似乎是完全水平的,所以我认为就是这种情况)。

You will be paying allocation costs, but as Ole says, if your app is performing well enough already, there's not much point in trying to optimize that.

您将支付分配成本,但正如Ole所说,如果您的应用程序已经表现良好,那么尝试优化它并没有多大意义。

#3


-1  

It depends, if the user has an iPhone 4 or iPhone 3GS it should be do-able but on the iPhone 3G it will result in a memory warning very quickly. iPhone 4 has 256mb of RAM for the apps ( 512 mb in total ) iPhone 3GS has 128mb for the apps, and 256 in total the iphone 3g only has 128mb and 64mb for the apps.. usually having around 40mb free when no apps are running.

这取决于,如果用户拥有iPhone 4或iPhone 3GS,它应该可以使用,但在iPhone 3G上它会很快产生内存警告。 iPhone 4为应用程序提供了256mb的RAM(总共512mb)iPhone 3GS的应用程序为128mb,总共256个iphone 3g只有128mb和64mb的应用程序..通常在没有应用程序运行时可以免费使用40mb 。

As apple says you should only allocate the memory you really need, and try to not use autorelease too much, because autorelease gives us an object being allocated while we don't really need it anymore

正如苹果所说,你应该只分配你真正需要的内存,并尝试不要过多地使用自动释放,因为autorelease给我们一个分配的对象,而我们不再需要它了

If the performance isn't too bad, I would try using less memory and allocating more when you really need it.

如果性能不是太差,我会尝试使用更少的内存并在您真正需要时分配更多内存。