UITextview: huge memory footprint

I'm encountering what seems like excessive memory usage in UITextView. I'm loading a 50Kb ascii text file into an NSString and assign it to the .text property of an empty UITextView component in my app. Assigning the NSString to to UITextView adds more than 100Mb to my memory footprint (2000x the actual text content).


Is there a way (a property?) to make the UITextView more memory efficient? I need the editability and scrolling of the contents, but it's plain text (not attributed) and I'm not using any complex layout or rich text features.


For context:

- I'm working in a memory-constrained environment (an audio unit extension) so every Mb of memory counts.

- I’m on iOS 12 and this behavior is seen on all iOS devices

- This doesn't seem to be related to the well-documented iOS11 UITextView memory leak, since I'm not releasing the component.

Replies

Could you show how you load the text into UITextView ?


Probably, you are creating some instance on each new line.


See here for a similar problem:

https://stackoverflow.com/questions/22520450/uitextview-consumes-huge-memory-in-ios

I’m fairly sure that’s not the case. It’s really the UItextView component itself allocating the memory


NSError* error = nil;

NSString* path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

// no problem up to this point.

myUITextView.text = content; // this adds over 110Mb of memory usage for a 50Kb text file.