objective c - Huge memory usage despite ARC -
i have following function opens image, scales , saves file.
-(void)writefiletoicon:(nsstring *)fullpath :(nsstring *)finalpath :(nssize)outputsize { nsdata *datatowrite; nsbitmapimagerep *rep; rep = [nsbitmapimagerep imagerepwithdata:[[self scaleimage:[[nsimage alloc]initwithcontentsoffile:fullpath] tosize:outputsize] tiffrepresentation]]; datatowrite = [rep representationusingtype:nspngfiletype properties:nil]; [datatowrite writetofile:finalpath atomically:yes]; } - (nsimage *)scaleimage:(nsimage *)image tosize:(nssize)targetsize { if ([image isvalid]) { nssize imagesize = [image size]; float width = imagesize.width; float height = imagesize.height; float targetwidth = targetsize.width; float targetheight = targetsize.height; float scalefactor = 0.0; float scaledwidth = targetwidth; float scaledheight = targetheight; nspoint thumbnailpoint = nszeropoint; if (!nsequalsizes(imagesize, targetsize)) { float widthfactor = targetwidth / width; float heightfactor = targetheight / height; if (widthfactor < heightfactor) { scalefactor = widthfactor; } else { scalefactor = heightfactor; } scaledwidth = width * scalefactor; scaledheight = height * scalefactor; if (widthfactor < heightfactor) { thumbnailpoint.y = (targetheight - scaledheight) * 0.5; } else if (widthfactor > heightfactor) { thumbnailpoint.x = (targetwidth - scaledwidth) * 0.5; } nsimage *newimage = [[nsimage alloc] initwithsize:nsmakesize(scaledwidth, scaledheight)]; [newimage lockfocus]; nsrect thumbnailrect; thumbnailrect.origin = nszeropoint; thumbnailrect.size.width = scaledwidth; thumbnailrect.size.height = scaledheight; [image drawinrect:thumbnailrect fromrect:nszerorect operation:nscompositesourceover fraction:1.0]; [newimage unlockfocus]; return newimage; } return nil; } return nil; } however each time function called, memory usage getting higher (up 5 gb 1000 calls).
issue drawrect function seems take lot of memory (according analyser) not release it.
how can "ask" arc release ?
thanks.
one may need @ whole code find problem. 1 idea follows, though: under arc cannot call "release" on objects, if set pointer object "nil", object released (unless other strong references object exist somewhere).
i suggest track code , make sure don't hold objects don't need anymore. if code encapsulated , structured, shouldn't happen.
if code designed, though, there chance amount of memory needed (unlikely, don't know without more details). if case, let system manage memory, release objects when appropriate. this, , try make optimizations somewhere if memory usage concern.
off-topic: these long nested if's multiple return points within method not idea; suggest reestructure code slightly. if write clearer code, you'll have more control on it, , find solutions problems faster.
Comments
Post a Comment