smorel
smorel

Reputation: 11

iOS : NSFileManager copyItemAtPath is memory intensive

I have an app that must copy a bunch of files from disk to disk. Those files are HD raw images so they are big. I wasn't suspecting that the NSFileManager copyItemAtPath: method would actually load the content in memory to copy it to the destination path.

I had to queue this copy tasks in a queue with 4 maximum concurrent operations to avoid busting my transiting memory quota and avoid crashes but as a result, I can have to wait 1 minute to finish copying all my files ...

Is their a more efficient way to clone a file from a path to another without having to load the content in memory ?

Here is a snapshot of what's happening while profiling with instrument:

enter image description here

In blue: the total living allocations

In Pink: the NSFileManager copyItemAtPath: impact on memory.

http://s9.postimg.org/io3654ty7/Screen_Shot_2013_06_18_at_12_43_03_PM.png

Upvotes: 1

Views: 700

Answers (2)

David Hoerl
David Hoerl

Reputation: 41652

Similiar to the comments, but use fopen, fread, fwrite as they handle buffering for you. Also one very important point - you should set the F_NOCACHE flat on BOTH the read and write file descriptors, otherwise you will greatly increase the system memory dedicated to the disk cache, which if too large can get you killed. If you search on file copy and the flag here you will surely find code to do this in the unix or C tagged items.

Upvotes: 0

Gianluca
Gianluca

Reputation: 133

NSFileManager copyItemAtPath:toPath:error: and copyItemAtURL:toURL:error: are the most low-level methods you can use to copy files in iOS.

So the answer is NO.

Upvotes: -1

Related Questions