iPhone的低内存占用的CSV解析器 [英] CSV parser with low memory footprint for iPhone

查看:103
本文介绍了iPhone的低内存占用的CSV解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Instruments测试我的应用程式后,我发现目前的 CSV解析器我使用有一个巨大的内存占用。是否有人对低内存占用的内存有建议?

After testing my app with Instruments I realized that the current CSV parser I use has a huge memory footprint. Does anybody have a recommendation for one with a low memory footprint?

推荐答案

您可能应该逐行比读取整个文件,解析它,并返回一个包含其中所有行的数组。在任何情况下,你链接的代码在循环中产生数以万计的临时对象,这意味着它将有非常高的内存开销。

You probably should do this row-by-row, rather than reading the whole file, parsing it, and returning an array with all the rows in it. In any case, the code you linked to produces zillions of temporary objects in a loop, which means it'll have very high memory overhead.

快速修复将是在循环的lop处创建一个NSAutoreleasePool,并在底部排出:

A quick fix would be to create an NSAutoreleasePool at the lop of the loop, and drain it at the bottom:

while ( ![scanner isAtEnd] ) {        
    NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

...一堆代码...

... bunch of code...

    [innerPool drain];
}

这将擦除临时对象,因此您的内存使用将是大小的数据,加上文件中每个字符串的对象(大约8字节*行*列)

This will wipe out the temporary objects, so your memory usage will be the size of the data, plus an object for each string in the file (roughly 8 bytes * rows * columns)

这篇关于iPhone的低内存占用的CSV解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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