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

发布于 2024-09-05 15:21:46 字数 184 浏览 5 评论 0原文

使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦旅人picnic 2024-09-12 15:21:46

您可能应该逐行执行此操作,而不是读取整个文件、解析它并返回包含其中所有行的数组。无论如何,您链接到的代码都会在循环中生成无数的临时对象,这意味着它将具有非常高的内存开销。

一个快速的解决方法是在循环的循环处创建一个 NSAutoreleasePool ,并在底部耗尽它:

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

...一堆代码...

    [innerPool drain];
}

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

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:

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)

想你只要分分秒秒 2024-09-12 15:21:46

还有一些其他 CSV 解析器可以尝试:

您可以尝试看看是否内存开销较低。

这些都不支持“基于事件”的解析。在基于事件的解析中,您永远不会将整个源文件加载到内存中,只需加载足够的文件来读取当前行(您也可以在下载时执行此操作)。您必须在读取每一行时对其进行处理,并确保在行之间释放源中的所有数据。

这将是理论上开销最低的解决方案。如果您确实需要低开销,您应该调整现有的解决方案来做到这一点(我对如何做到这一点没有任何建议)。

There are some other CSV parsers to try:

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).

蛮可爱 2024-09-12 15:21:46

它不是 CSV 解析器,但我的开源 Cocoa ParseKit 框架有一个强大/方便/可配置的字符串标记器,对于 CSV 或其他类型的解析/标记化可能很方便。

框架:

http://parsekit.com

一些使用文档:

http://parsekit.com/tokenization.html

PKTokenizer 类:

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

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.

The framework:

http://parsekit.com

Some usage documentation:

http://parsekit.com/tokenization.html

The PKTokenizer class:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文