 |
 |
|
 |
07-05-2009, 01:45 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
|
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
|
|
|
07-05-2009, 04:11 PM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
|
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.
__________________
|
|
|
07-05-2009, 04:22 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
|
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.
|
|
|
07-05-2009, 09:10 PM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
|
Quote:
Originally Posted by NeoNate
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?
__________________
|
|
|
07-06-2009, 02:15 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
|
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
|
|
|
07-06-2009, 07:48 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Oct 2008
Location: Bengaluru
Posts: 123
|
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.
|
|
|
07-06-2009, 01:38 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
|
Quote:
Originally Posted by bharath2020
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.
|
|
|
07-06-2009, 03:30 PM
|
#8 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
|
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?
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 364 |
| 36 members and 328 guests |
| activ8, andrei_c, aniuco, ansonl, ashishjraval, carendt242, coolblue, DonomaGames, Ed99, erickkung, exorcyze, goodcode, Gudus, guyhundere, Hololont, hyang, ifan, iisword, jamison, JasonR, JoshuaCaputo, jwutke, lucasweb, malebolgia, marchinram, muzammil, paymaster, R1kR1k, rendezvouscp, Rudy, sanjeevrao.lade, Scenario, TapTouchClick, tungbh, vinitks, yeurh121 |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,330
Threads: 39,127
Posts: 171,553
Top Poster: smasher (2,577)
|
| Welcome to our newest member, yeurh121 |
|