Skip to main content

Posts

Showing posts with the label memory-management

How to download a large file with the iPhone SDK and avoid memory usage issues?

I'm using the NSURLConnection class to download a large file in my iPhone application, but it crashes every so often because it's using too much memory. I'm doing the usual NSURLConnection usage, to append the received data to a NSMutableData object. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.fileData appendData:data]; } Then after I finish downloading the whole file, I save it to a local temporary file, and read it as a mapped file like this: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // save the downloaded data into a temporary file NSString *tempPath = NSTemporaryDirectory(); NSString *tempFile = [tempPath stringByAppendingPathComponent:@"temp.pdf"]; [self.fileData writeToFile:tempFile atomically:YES]; NSData *mappedData = [NSData dataWithContentsOfMappedFile:tempFile]; NSURL *baseURL = [NSURL URLWithString:@"http://mydomain.com"]; [webView loadD

In objective-C (iphone), how do I manage the memory of "@protocol" references?

I thought I had a pretty good handle on memory management for objective-c, but I can't figure out the following situation: @protocol MyProtocol @end @interface MyObject : NSObject { id<MyProtocol> reference; } @property (nonatomic, retain) id<MyProtocol> reference; @end @implementation MyObject @synthesize reference; -(void) dealloc { [reference release]; [super dealloc]; } ... @end This gives me a " warning: '-release' not found in protocol(s) ". Can I safely ignore this error? Or am I doing something horribly wrong? Source: Tips4all

Why does java wait so long to run the garbage collector?

I am building a Java web app, using the Play! Framework . I'm hosting it on playapps.net . I have been puzzling for a while over the provided graphs of memory consumption. Here is a sample: The graph comes from a period of consistent but nominal activity. I did nothing to trigger the falloff in memory, so I presume this occurred because the garbage collector ran as it has almost reached its allowable memory consumption. My questions: Is it fair for me to assume that my application does not have a memory leak, as it appears that all the memory is correctly reclaimed by the garbage collector when it does run? (from the title) Why is java waiting until the last possible second to run the garbage collector? I am seeing significant performance degradation as the memory consumption grows to the top fourth of the graph. If my assertions above are correct, then how can I go about fixing this issue? The other posts I have read on SO seem opposed to calls to System.gc()

Is volatile expensive?

After reading http://gee.cs.oswego.edu/dl/jmm/cookbook.html about the implementation of volatile, especially section "Interactions with Atomic Instructions" I assume that reading a volatile variable without updating it needs a LoadLoad or a LoadStore barrier. Further down the page I see that LoadLoad and LoadStore are effectively no-ops on X86 CPUs. Does this mean that volatile read operations can be done without a explicit cache invalidation on x86, and is as fast a normal variable read (disregarding the reordering constraints of volatile)? I believe I don't understand this correctly. Could someone care to enlighten me? EDIT: I wonder if there are differences in multi-processor environments. On single CPU systems the CPU might look at it's own thread caches, as John V. states, but on multi CPU systems there must be some config option to the CPUs that this is not enough and main memory has to be hit, making volatile slower on multi cpu systems, right? PS: On

Memory is not released even though I explicitly release it

I am in the process of optimizing my app and making sure memory management is properly implemented. As I found the didUnload / dealloc / willAppear not reliable for implementing my memory cleanup, I decided to implement my own method so I can have full control of this memory management. Definition of my arrays in the header file @property (retain) NSMutableArray *selectedCardIDs; @property (retain) NSMutableArray *selectedRowArray; @property (retain) NSMutableArray *cardArray; @property (retain) NSMutableArray *cardIDArray; Here the release method: - (void) willReleaseObjects { [self.aCopyOfCardIDArray release]; [self.listOfItems release]; [self.aCopyListOfItems release]; [self.selectedCardIDs release]; [self.selectedRowArray release]; [self.cardArray release]; [self.cardIDArray release]; } The arrays can get very large (> 1'000 entry each), why a release of those arrays is essential after the view is unloaded. I explicitly call this function in the IBAction method, su

Drawables resources management in android

I have one question. I made quick search on the site, but don't found answer. We develop application that running android 2.2 and higher. For views customization we use many drawables, that used in such way: <LinearLayout ... android:background="@drawable/some_drawable"/> We use maps too and manipulate with many data in the memory, and our app got a heavy. On top devices, it work's great,but on other we got OutOfMemory exception after some minutes of using our application. It's look as we have memory leaks. I'm trying reduce the memory usage of our app. question, do we need manualy cleaning resources on destroing our activities: removes drawables for view, or system made it for us?

iPhone - How to handle errors at runtime

When writing code, there are many situations that must be treated as runtime errors : an alloc/init returns nil, a resource is not found, a [someClass canDoThis] returns NO for an absolutely-needed feature where YES would be the natural answer, ... For all these situations, I have written an exitWithMessage routine (that displays an alert box), and each class has a kill method that frees allocated memory. So... When in an init method, you have these kind of exceptions, I supposed you could do : [self kill]; [OneClass exitWithFatalErrorMessage]; return nil; - (void) exitWithFatalErrorMessage:(NSString*)message { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(@"Error" @"ErrorMessages", @"") message:message delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"Understood", @"ErrorMessages", @"") otherButtonTitles: nil]; [alert show]; [alert release]; } - (void)alertV

iPhone - How to test allocated objects returned by each memory call?

When writing an app, you always have to write alloc/inits, get autoreleased datas returned by the framework classes, ... This may be 70% of the code, almost each single line of what you write... So... How do those returned object must be tested, to know if each of these calls have returned a correct object ? Testing the returned value each time, for each call, and treating the exception if you get nil where you expected an allocated object ? Letting the app crash ? How this must be done ?