UITextView:大量的内存使用情况 [英] UITextView: Huge Memory Usage

查看:83
本文介绍了UITextView:大量的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITextView中遇到了似乎过多的内存使用情况.我正在将50Kb的ASCII文本文件加载到NSString中,并将其分配给我应用中空UITextView组件的.text属性.这立即使我的内存占用量增加了100Mb(实际文本内容的2000倍).

I'm encountering what seems like excessive memory usage in a 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. This immediately adds more than 100Mb to my memory footprint (2000x the actual text content).

是否有一种方法(属性或转换)可以使UITextView的内存使用效率更高?我需要内容的可编辑性和滚动性,但是它是纯文本(未指定属性),并且我没有使用任何复杂的布局或富文本功能.

Is there a way (a property or conversion) 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.

对于上下文:我在内存受限的环境(音频单元扩展)中工作,因此每Mb的内存都至关重要.

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

这似乎与记录良好的iOS11 UITextView内存泄漏无关,因为我没有发布该组件.

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

NSError* error = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
myUITextView.text = content; // this adds over 110Mb of memory usage for a 50Kb text file.

推荐答案

OP完全正确!

我们发现,如果您添加到文本视图中,则在"+ =的意义上,它将占用大量的内存.

We found that if you add to a text view, in the "+=" sense, it uses insanely large amounts of memory.

我们有这样的文本视图

 @IBOutlet var showValues: UITextView!

它会显示一些文本行(例如10至100行左右),每行只是一个整数.

and it would display some lines (say, 10 to 100 or so lines) of text, each line just being an integer.

所以我每次都非常简单地附加"文本,就像这样:

So I very simply "appended" the text each time, like this:

 latestValue: String = .. // something like "14.4 \n"
 showValues.text += latestValue

如果您只是尝试此操作,则每行会增加大约1/2 mb !!!!!!!!!

If you simply try this it adds about 1/2 mb every line!!!!!!!!!

也许这是iOS中的一个普通错误?

Perhaps this is a plain bug in iOS?

怎么可能?测试起来很容易!

How could it possibly be? It's easy to test this!

这篇关于UITextView:大量的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆