且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-22 10:03:40

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

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)