4

I'm new to iOS programming and I have just built an iPhone app that can ask user a question and return an answer. The building environment is OS X 10.9 and Xcode 5.0.2. Every time I starts the iPhone simulator, the Debug Navigator shows that the memory usage is 13.5mb but it keeps going up even after I returned to Home screen. After a minute the memory usage will become stabilized around 17.5mb. Is this a normal behavior or I need to add some memory management code?

#import "QuizViewController.h"

@interface QuizViewController ()

@property (nonatomic) int currentQuestionIndex;
@property (nonatomic, copy) NSArray *questions;
@property (nonatomic, copy) NSArray *answers;

@property (nonatomic,weak) IBOutlet UILabel *questionLable;
@property (nonatomic,weak) IBOutlet UILabel *answerLable;

@end

@implementation QuizViewController

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        self.questions = @[@"From what is cognac made?",
                           @"What is 8 + 8 ?",
                           @"What is the capital of Minnesota?"];
        self.answers = @[@"Grapes",
                         @"16",
                         @"St.Paul"];
    }

    return self;
}

- (IBAction)showQuestion:(id)sender
{
    self.currentQuestionIndex++;
    if (self.currentQuestionIndex == [self.questions count]){
        self.currentQuestionIndex = 0;
    }
    NSString *question = self.questions[self.currentQuestionIndex];
    self.questionLable.text = question;
    self.answerLable.text = @"???";
}
- (IBAction)showAnswer:(id)sender
{
    NSString *answer = self.answers[self.currentQuestionIndex];
    self.answerLable.text = answer;
}

@end
wz366
  • 2,840
  • 6
  • 25
  • 34
  • why are you doing "`- (instancetype) `" instead of "`- (id)`" for your UIViewController init method? – Michael Dautermann Mar 10 '14 at 01:09
  • 1
    If the memory usage is stabilized, then you're probably ok. If it keeps going up as you go back and forth between your controllers, then you're doing something wrong. – rdelmar Mar 10 '14 at 01:30
  • 1
    Michael, this link says using instancetype is preferred than using id. http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id – wz366 Mar 10 '14 at 01:32

1 Answers1

1

Memory Management is automatic with ARC. Are you getting did receive memory warning log in output? If not then you are fine. Other than that I think this is normal.

Charlie
  • 222
  • 3
  • 20
  • +1 for mentioning ARC, you beat me to it by seconds. – Alejandro Mar 10 '14 at 01:08
  • I didn't get any warning log. Dose ARC work just like the Garbage Collector in java? Are there any cases in which I have to free() memory by myself? Thanks! – wz366 Mar 10 '14 at 01:21
  • I don't know much of java so I can not say exactly but ARC (Automatic Reference Counting) makes it so that you do not have to release or dealloc objects after creating them. That is the short version of what it does. http://en.wikipedia.org/wiki/Automatic_Reference_Counting that will give you a little more info on it. Mainly as long as you are not getting a "Received memory warning." log then there is nothing you need to worry about – Charlie Mar 10 '14 at 01:25
  • 1
    Yes, it is like Garbage Collector, and I don't agree that you shouldn't worry if you didn't receive a memory warning. Usually the memory leak is not big enough to receive a memory warning in a short period of time. There are some iOS objects that aren't managed by ARC, so even when they're dismissed, their memory will hold if not explicitly released e.g. Core Graphics objects. Anyway, the author says the memory grows up and then stabilise - that's normal and fine! – Gobe Mar 10 '14 at 04:05
  • 2
    iOS Simulator will NOT issue Memory Warnings. And Memory Warnings are not a good way to verify that memory usage is okay. The amount of used up memory after which a warning is sent is very different between devices. It's possible that an app does not run at all on devices with lower memory (iPhone 4 (512 MB RAM), or even 3GS (256 MB RAM)) while it never triggers a memory warning on devices like the iPhone 5 (1 GB RAM). – Matthias Bauch Mar 10 '14 at 07:06