Page 1 of 1

Delay iOS app termination due to low memory condition

PostPosted: Tue Apr 30, 2019 5:59 am
by Georgia96698
I'm developing a linguistic app that, occasionally, needs to allocate a large amount of memory for a certain period of time (normally under 30 seconds). Unfortunately, iOS sends low memory warnings and sometimes terminates the app (roughly 5% of the times). Of course I can't rely on chance so I have to fix it. I'm trying to optimize the algorithm (it's basically looking for spelling and sound similarities using Levenshtein distance) but I'm afraid that using the file system instead of the memory would make the processing too slow. Is there any way to respond to low memory warnings if I know that the memory will be released soon? Any other suggestions?

A few details:

    the core processing is done in the background in an NSOperation
    the algorithm is written in C++ for performance reasons, and it uses C++ strings and mallocs.
    the same algorithm ported to Objective-C is 10x slower, even though I haven't investigated the reasons
    the app is not leaking memory. After the long processing, all the memory is properly released
    the app is iPad only

EDIT: the bulk of the storage is a C++ vector with about 200K English words. I have to compare these words with some potential candidates for both spelling and phonetic similarities. For each candidate I have to iterate through the whole list of words and calculate Levenshtein Distance. The distance is calculated using a similarity matrix which statically allocated in a function. I'm still puzzled at why I start with a vocabulary which is about 2 MB and end up with 70+ MBs of live bytes during the processing.

Re: Delay iOS app termination due to low memory condition

PostPosted: Mon May 06, 2019 12:15 pm
by ccnut
Georgia96698 wrote:I'm still puzzled at why I start with a vocabulary which is about 2 MB and end up with 70+ MBs of live bytes during the processing.


I think that is pretty important to figure out. Also 70MB seems a little low to be causing memory pressure issues.

But you might consider using the ramfs. I'm not aware of the API to do so, but any "temporary file"-type API should do the trick. All the files will live in a filesystem that is entirely kept in RAM, so it never has the latencies encountered when writing files to the drive.