简体   繁体   English

适用于iPhone的内存占用较少的CSV解析器

[英]CSV parser with low memory footprint for iPhone

After testing my app with Instruments I realized that the current CSV parser I use has a huge memory footprint. 在使用Instruments测试我的应用程序后,我意识到我使用的当前CSV解析器具有巨大的内存占用。 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. 在任何情况下,您链接的代码在循环中产生数以万计的临时对象,这意味着它将具有非常高的内存开销。

A quick fix would be to create an NSAutoreleasePool at the lop of the loop, and drain it at the bottom: 快速解决方法是在循环的顶部创建一个NSAutoreleasePool,并将其排到底部:

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

... bunch of code... ......一堆代码......

    [innerPool drain];
}

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) 这将消除临时对象,因此您的内存使用量将是数据的大小,加上文件中每个字符串的对象(大约8个字节*行*列)

There are some other CSV parsers to try: 还有一些其他CSV解析器可供尝试:

You could experiment to see if either is lower memory overhead. 您可以尝试查看是否有较低的内存开销。

Neither of these supports "event based" parsing. 这些都不支持“基于事件”的解析。 In event based parsing, you never load the whole source file into memory, just enough of the file to read the current row (you can also do this in-progress on a download). 在基于事件的解析中,您永远不会将整个源文件加载到内存中,只需要足够的文件来读取当前行(您也可以在下载时进行此操作)。 You must handle each row as it is read and make certain all data from the source is freed between rows. 您必须在读取每一行时处理它们,并确保源中的所有数据都在行之间释放。

This would be the theoretical lowest overhead solution. 这将是理论上最低的开销解决方案。 If you really needed low overhead, you should adapt an existing solution to do that (I don't have any advice on how this would be done). 如果你真的需要低开销,你应该调整现有的解决方案来做到这一点(我没有任何关于如何做到这一点的建议)。

It's not a CSV parser, but my open source Cocoa ParseKit framework has a powerfull/convenient/configurable string tokenizer which might be handy for CSV or other types of parsing/tokenizing. 它不是一个CSV解析器,但我的开源Cocoa ParseKit框架有一个功能强大/方便/可配置的字符串标记器,可以方便CSV或其他类型的解析/标记。

The framework: 框架:

http://parsekit.com http://parsekit.com

Some usage documentation: 一些使用文档:

http://parsekit.com/tokenization.html http://parsekit.com/tokenization.html

The PKTokenizer class: PKTokenizer类:

http://github.com/itod/parsekit/blob/master/include/ParseKit/PKTokenizer.h http://github.com/itod/parsekit/blob/master/src/PKTokenizer.m http://github.com/itod/parsekit/blob/master/include/ParseKit/PKTokenizer.h http://github.com/itod/parsekit/blob/master/src/PKTokenizer.m

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

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