[英]Memory being allocated and not released each time call very simple method
I have the following method that is called when the delete button is pressed in an IOS keyboard app extension: 我在iOS键盘应用扩展中按下删除按钮时会调用以下方法:
func delete()
{
dispatch_async(dispatch_get_main_queue(),
{
for _ in 1..<50
{
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
print("Deletion End")
})
}
However, each time this method is called my keyboard's memory usage goes up by 1-2mb and it does not come back down. 但是,每次调用此方法时,键盘的内存使用量都会增加1-2mb,并且不会降低。 This quickly results in a crash.
这很快导致崩溃。
I am not allocating or de-allocating any objects in this thread, so I am not sure why there is a massive memory leak. 我没有在此线程中分配或取消分配任何对象,所以我不确定为什么会有大量的内存泄漏。
UPDATE: 更新:
I changed dispatch_get_main_queue()
to a static variable: 我将
dispatch_get_main_queue()
更改为静态变量:
let mainqueue = dispatch_get_main_queue()
and used mainqueue
inplace of dispatch_get_main_queue
but this did not do anything. 并使用
mainqueue
了dispatch_get_main_queue
但这没有做任何事情。
UPDATE 2: 更新2:
I added a variable called tempProxy
that is created each time delete is called: 我添加了一个名为
tempProxy
的变量,该变量在每次调用delete时创建:
let tempProxy = (self.textDocumentProxy as UIKeyInput)
Then in dispatch_async
I used this: let tempProxy = (self.textDocumentProxy as UIKeyInput)
然后在dispatch_async
我使用了这个:
tempProxy.deleteBackward()
I did this because I heard that having references to self
could cause memory retention. 我这样做是因为我听说引用
self
会导致内存保留。
UPDATE 3: 更新3:
I changed: 我变了:
(self.textDocumentProxy as UIKeyInput).deleteBackward()
to a print()
statement. 到
print()
语句。
The memory usage was now a lot less. 现在,内存使用量大大减少了。 However, I was confused as to why the memory was still not being released.
但是,我对于为什么仍不释放内存感到困惑。
原来我启用了NSZombie对象,每次我调用deleteBackward()
时,它都会永久增加内存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.