Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-05-2009, 01:45 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
Question Memory keeps going up

I have been having a long term issue with my app's memory increasing, so today have stripped my code back to basics but still have the same problem.

My retain counts remain constnact (2 for each MyImage[i] and 3 for "self"]. There are no memory leaks detected by Intruments, but it does report that the memory keeps increasing.

Can anyone point out anything I am not doing right? This issue has brought my development to a halt and I've not made any real progress for about three weeks as a result.

Code:
#import "TestView.h"
#import <QuartzCore/QuartzCore.h>

@interface TestView()
	@property (nonatomic, retain) UIImageView *MyImage;
@end


@implementation TestView

@dynamic MyImage;
int NextImage = 1;
int PrevImage = 0;
int counter = 0;


- (id)initWithCoder:(NSCoder*)coder 
{
	if (self = [super initWithCoder:coder])
	{
		MyImage[1] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image1.png"]];
		MyImage[2] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image2.png"]];
		MyImage[3] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image3.png"]];
		MyImage[4] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image4.png"]];
		MyImage[5] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image5.png"]];
		MyImage[6] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image6.png"]];
		MyImage[7] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image7.png"]];
		MyImage[8] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image8.png"]];
		MyImage[9] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image9.png"]];
		MyImage[10] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image10.png"]];
		
		for(int i = 1; i <= 10; i++)
		{
			[self addSubview:MyImage[i]];
			MyImage[i].center = CGPointMake(999,999);
		}

		[NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
	}
	return self;
}


- (void)gameLoop
{
	counter = counter + 1;
	if(counter > 10)
	{counter = 1;}
	
	switch(counter)
	{
		case 1:		NextImage = 1; break;
		case 2:		NextImage = 2; break;
		case 3:		NextImage = 3; break;
		case 4:		NextImage = 4; break;
		case 5:		NextImage = 5; break;
		case 6:		NextImage = 6; break;
		case 7:		NextImage = 7; break;
		case 8:		NextImage = 8; break;
		case 9:		NextImage = 9; break;
		case 10:	NextImage = 10; break;
	}		
	
	UIImageView *imageView = MyImage[PrevImage];
	imageView.center = CGPointMake(999,999);
			
	UIImageView *imageView2 = MyImage[NextImage];
	imageView2.center = CGPointMake(160,240);

	PrevImage = NextImage;	 
}


- (void)dealloc 
{
	[MyImage[50] release];
	[super dealloc];
}
	

@end
Thanks in advance,
Nathan
NeoNate is offline   Reply With Quote
Old 07-05-2009, 04:11 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

Does the memory increase every time the loop fires, or only when creating new TestViews?

Where do you declare the array MyImage[] ? I think you're walking all over memory that you did not allocate. That's a very big deal.

Also, [MyImage[50] release] does not release all of the images - it only releases the one at index 50. That's one problem, if you're repeatedly creating and removing "TestView" objects. Better to release each imageView right after you add it as a subview.

You could also replace your "switch" statement with "NextImage = counter" , but that's not a memory problem.
__________________
smasher is offline   Reply With Quote
Old 07-05-2009, 04:22 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
Default

Hi Smasher,

Thanks for the response. Yeap the memory increases with every loop. Do I need to release after every image? When does dealloc get called? I thought it was when the app exits.

I am declaring the following within my header:

Code:
@interface TestView : UIView 
{
	UIImageView *MyImage[50];
}
@end

Last edited by NeoNate; 07-05-2009 at 04:42 PM.
NeoNate is offline   Reply With Quote
Old 07-05-2009, 09:10 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

Quote:
Originally Posted by NeoNate View Post
Hi Smasher,

Thanks for the response. Yeap the memory increases with every loop.
The net bytes, or the overall bytes? overall bytes is fine, net bytes (and a climbing blue graph at the top) is a problem.

Quote:
Do I need to release after every image? When does dealloc get called?
Dealloc is called by the system when the object's retainCount becomes zero. This should happen when you release the testView and you remove it from the superview.

If this view is only used once for the lifetime of your program then this probably isn't the issue, but you should release all of the images in the dealloc. You could use a loop.

MyImage declaration looks fine. I don't see the purpose of these lines, though:

Code:
@interface TestView()
	@property (nonatomic, retain) UIImageView *MyImage;
@end

@dynamic MyImage;
You are not using MyImage as a property, and I doubt you're creating dynamic methods for it.

I really don't see anything in gameLoop that should cause a memory increase. If you sort the objectAlloc display by "Net #" , can you tell what kind of object is climbing up the chart?
__________________
smasher is offline   Reply With Quote
Old 07-06-2009, 02:15 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
Default

Thanks I have removed those, it is still climbing though (at a rate of 75 per loop in the "Net #" value for GeneralBlock0).

I have attached a screenshot of the stack:





Thanks for your help.

Last edited by NeoNate; 07-06-2009 at 02:36 AM. Reason: added additional image
NeoNate is offline   Reply With Quote
Old 07-06-2009, 07:48 AM   #6 (permalink)
Registered Member
 
bharath2020's Avatar
 
Join Date: Oct 2008
Location: Bengaluru
Posts: 123
Default

It seems that you are checking "All Objects created" instead of "Created and Still living"

Please select the "Created and still living" radio button on the left side of instruments and check again.
bharath2020 is offline   Reply With Quote
Old 07-06-2009, 01:38 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
Default

Quote:
Originally Posted by bharath2020 View Post
It seems that you are checking "All Objects created" instead of "Created and Still living"

Please select the "Created and still living" radio button on the left side of instruments and check again.
Thanks for the suggestion but the results are the same either way.
NeoNate is offline   Reply With Quote
Old 07-06-2009, 03:30 PM   #8 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
Default

It seems my #NET value is going up but the NET BYTES stays a zero, even if I leave it running for an hour!

Is this what is supposed to happen? or should it just be the OVERALL value that should never decrease?
NeoNate is offline   Reply With Quote
Old 07-10-2009, 02:36 PM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

Recap for the lurkers: you're seeing a bunch of generalblock-0 's when you set the position of a view; this post suggests it may be a bug in Instruments, not a bug in Quartz:

Thousands of new 0 size objects being added to my net total every second, should I be worried? - Stack Overflow

Apple suggests that if you think you've found an error in their memory management, check it with the Memory Monitor instrument:

https://devforums.apple.com/message/88251
__________________

Last edited by smasher; 07-10-2009 at 03:09 PM. Reason: add Memory Monitor
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,330
Threads: 39,127
Posts: 171,553
Top Poster: smasher (2,577)
Welcome to our newest member, yeurh121
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:06 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0