![](/img/trans.png)
[英]IOS:Memory footprint increases when switching between views (ARC enabled)
[英]iOS: How to remove object from memory with ARC enabled?
我正在使用iOS 5 SDK開發iOS應用程序,啟用了自動引用計數。 但我有一個特定的對象正在大量創建,必須在一秒鍾之后釋放,否則設備將變得非常慢。 看起來它們沒有被釋放,因為設備非常慢。 有沒有辦法在啟用ARC時手動釋放對象?
編輯:我的代碼,這被稱為每秒200次生成火花。 它們在0.8秒后淡出,因此在那之后它們沒用了。
int xanimationdiff = arc4random() % 30;
int yanimationdiff = arc4random() % 30;
if (arc4random()%2 == 0) {
xanimationdiff = xanimationdiff * -1;
}
if (arc4random()%2 == 0) {
yanimationdiff = yanimationdiff * -1;
}
Sparkle *newSparkle = [[Sparkle alloc] initWithFrame:CGRectMake(20 + arc4random() % 280, 20, 10, 10)];
//[newSparkle setTransform:CGAffineTransformMakeRotation(arc4random() * (M_PI * 360 / 180))]; //Rotatie instellen (was niet mooi, net sneeuw)
[self.view addSubview:newSparkle];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[newSparkle setFrame:CGRectMake(newSparkle.frame.origin.x - xanimationdiff, newSparkle.frame.origin.y - yanimationdiff, newSparkle.frame.size.width, newSparkle.frame.size.height)];
newSparkle.alpha = 0;
[UIView commitAnimations];
閃光對象代碼:
#import "Sparkle.h"
@implementation Sparkle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"sparkle.png"]]];
}
return self;
}
@end
Object* myObject = [[Object alloc] init];
myObject = nil; // poof...
編輯:您無法直接控制對象何時被釋放但您可以間接導致它發生。 怎么樣? 記住ARC確實做了什么。 與人類編碼約定不同,ARC會解析您的代碼並插入發布語句,因為即使是對象也可以發布。 這樣可以立即釋放內存以進行新的分配,這非常棒/必要。 意思是,將對象設置為nil,或者只是允許變量超出范圍......導致A 0 RETAIN COUNT的事情迫使ARC在其中放置其釋放調用。 它必須......因為否則會泄漏。
只需使用@autoreleasepool {...}語句包圍將要調用200次的代碼部分。 這將導致內存立即被釋放,而不是等待控件一直向上返回事件鏈到頂級自動釋放池。
我找到了答案,實際上真的很愚蠢。 我沒有從superview中刪除閃光。 現在我用計時器在0.8秒后刪除它們,它再次表現很好:)
使用ARC,您無法調用 dealloc
, release
或retain
,盡管您仍然可以保留和釋放CoreFoundation對象(注意:您可以為自己的自定義子類實現dealloc
方法,但不能調用super dealloc
)。 所以簡單的答案是“不”,遺憾的是,在使用ARC時,您無法手動釋放對象。
我會仔細檢查你是否確定它們沒有被釋放,因為理論上如果你不再引用一個物體它應該被釋放。 創建它們后,如何處理這些對象? 你只需創建它們然后立即銷毀它們?
也許您可以發布您正在使用的代碼/屬性聲明 - 這些weak
或strong
引用對象?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.