如何释放静态Objective-C变量

时间:2021-08-06 05:38:41

The * question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release.

*问题“在定义缓存变量时在objective-c中使用static关键字”引用了Xcode的TableViewSuite的示例4中的代码,该代码定义了静态NSDateFormatter并调用alloc但从不调用release。

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

不应该发布静态变量吗?如果是,代码应该在何处发布?如果不是,为什么不呢?

2 个解决方案

#1


16  

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

不应该发布静态变量吗?如果是,代码应该在何处发布?如果不是,为什么不呢?

It depends. If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway). If, however, the value of the static variable changes, then yes, the previous object should be released when the static variable is set to a new object.

这取决于。如果变量只初始化一次,并且应该在应用程序的生命周期内保持不变,那么不,它不应该被释放(无论如何,当应用程序退出时,它的内存基本上将被释放)。但是,如果静态变量的值发生更改,则yes,当静态变量设置为新对象时,应释放上一个对象。

#2


2  

As the accepted answer to that question already states, releasing static variables is impossible. They act like global variables that are only visible to your function with a lifetime as long as your program.

由于该问题的已接受答案已经说明,释放静态变量是不可能的。它们就像全局变量一样,只有你的程序才能在你的函数中看到它。

If you are concerned about bloat because of variables that that static variable holds on to, then you should (somehow) release those references. Thus, for example, if your static variable is an NSMutableArray, and you keep adding objects inside, it will always keep growing, unless you, at some point, empty the array.

如果你因为静态变量所持有的变量而担心膨胀,那么你应该(以某种方式)释放这些引用。因此,例如,如果您的静态变量是NSMutableArray,并且您继续在其中添加对象,它将始终保持增长,除非您在某个时候清空该数组。

#1


16  

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

不应该发布静态变量吗?如果是,代码应该在何处发布?如果不是,为什么不呢?

It depends. If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway). If, however, the value of the static variable changes, then yes, the previous object should be released when the static variable is set to a new object.

这取决于。如果变量只初始化一次,并且应该在应用程序的生命周期内保持不变,那么不,它不应该被释放(无论如何,当应用程序退出时,它的内存基本上将被释放)。但是,如果静态变量的值发生更改,则yes,当静态变量设置为新对象时,应释放上一个对象。

#2


2  

As the accepted answer to that question already states, releasing static variables is impossible. They act like global variables that are only visible to your function with a lifetime as long as your program.

由于该问题的已接受答案已经说明,释放静态变量是不可能的。它们就像全局变量一样,只有你的程序才能在你的函数中看到它。

If you are concerned about bloat because of variables that that static variable holds on to, then you should (somehow) release those references. Thus, for example, if your static variable is an NSMutableArray, and you keep adding objects inside, it will always keep growing, unless you, at some point, empty the array.

如果你因为静态变量所持有的变量而担心膨胀,那么你应该(以某种方式)释放这些引用。因此,例如,如果您的静态变量是NSMutableArray,并且您继续在其中添加对象,它将始终保持增长,除非您在某个时候清空该数组。