问题
I'm developing a game for iOS and the game is being terminated after some game screens. I launched instrument in Xcode and I realized the memory keeps growing between my screens. ARC is enabled.
I used the mark generation featured to try to track the abandoned memory between the first two screens as you can see in the image below.
Generations A is taken in TitleScreen right after firing the app. B, C, and D are from the TitleScreen right after coming back from ChoosePlayer.
These screen have a few UIImageViews and UIButtons created via InterfaceBuilder I removed all programmatically created views. As you can see there seems to be a lot of abandoned memory, but I don't know how to track them since the stack comes from a intern API call.
I don't have any reference to any other view controller as well. (no strong cycle reference).
Below is the full code for the TitleScreen view controller.
I'm sure this must be a stupid error, but I can't find it. Any Ideas? Thanks.
@interface SMTitleScreenViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonPlay;
@property (weak, nonatomic) IBOutlet UIButton *buttonCamera;
@property (weak, nonatomic) IBOutlet UIImageView *titleBG1;
@property (weak, nonatomic) IBOutlet UIImageView *titleBG;
- (IBAction)onButtonPlay:(id)sender;
- (IBAction)onButtonCamera:(id)sender;
@end
@implementation SMTitleScreenViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor* color = [UIColor colorWithRed:0.2509f green:0.1176f blue:0.0745f alpha:1.0f];
UIFont* font = [UIFont fontWithName:@"Jungle Roar" size:BUTTON_FONT_SIZE];
NSString* playString = NSLocalizedString(@"Play", @"");
NSString* cameraString = NSLocalizedString(@"Camera", @"");
[self.buttonPlay setTitle:playString forState:UIControlStateNormal];
[self.buttonPlay setTitle:playString forState:UIControlStateHighlighted];
[self.buttonPlay setTitleColor:color forState:UIControlStateNormal];
[self.buttonPlay setTitleColor:color forState:UIControlStateHighlighted];
self.buttonPlay.titleLabel.font = font;
[self.buttonCamera setTitle:cameraString forState:UIControlStateNormal];
[self.buttonCamera setTitle:cameraString forState:UIControlStateHighlighted];
[self.buttonCamera setTitleColor:color forState:UIControlStateNormal];
[self.buttonCamera setTitleColor:color forState:UIControlStateHighlighted];
self.buttonCamera.titleLabel.font = font;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)onButtonPlay:(id)sender
{
[self performSegueWithIdentifier:@"titleToChooseAnt" sender:self];
}
- (IBAction)onButtonCamera:(id)sender
{
}
@end
回答1:
If you're concerned about memory not getting released, I think the analysis will be simplified if you focus on the generations after you return to TitleScreen. Looking at generations when you're at ChoosePlayer are probably only confusing the situation.
So, fire up the app and get the initial title screen, do generation A. Go to ChoosePlayer (don't do a generation) and then return to TitleScreen again (wait a second) and do generation B. Repeat that process for generation C and D.
Having done that, focus your attention on generations C and D. Don't worry about A (which has everything consumed during startup), nor even B (which might include stuff that might be cached). You really want to focus your attention on generations after you return the second, third, and subsequent times.
来源:https://stackoverflow.com/questions/21099200/cant-locate-abandoned-memory-in-ios-app