-2

i am apending the string, i running loop so many times app was crashed. it's show error message how i mange error message is malloc: * mmap(size=16777216) failed (error code=12) error: can't allocate region set a breakpoint in malloc_error_break to debug * Terminating app due to uncaught exception 'NSMallocException', reason: 'Out of memory. We suggest restarting the application. If you have an unsaved document, create a backup copy in Finder, then try to save.' and my code is ` NSMutableString * str = [NSMutableString stringWithCapacity:100000];

int i;
for(i=0;i<1000000;i++){
    [str appendString:@"abcd"];

}`

  • This is around 16mb in one string, need? – Anoop Vaidya Apr 05 '13 at 05:00
  • Please have a look here [Max Size limit of NSString](http://stackoverflow.com/questions/6482641/what-is-the-maximum-length-of-an-nsstring-object) – Anoop Vaidya Apr 05 '13 at 05:03
  • 1
    It's pretty simple; you used all the available memory. If you were doing something meaningful, there are ways to acquire a larger heap for your program, but it appears you're just testing things out. I don't think there is really a problem here; you've just discovered the limitations of your computer. – Parker Kemp Apr 05 '13 at 05:19

1 Answers1

4

Without knowing the details of what you are actually doing, it is impossible to say.

In general, when you run out of memory, the answer is to use less memory.

For an operation involving a massive chunk of data, you would need to move to some operation that buffers through the disk. This can be done via any number of means. Which one is used depends on the details of whatever you are trying to do.

If you really are just appending text to a buffer, then open a file descriptor (or NSFileHandle) and write to that instead.

bbum
  • 162,346
  • 23
  • 271
  • 359