0

When using this code in xcode for iphone I get an allocation

region error. (Using simulater)

NSString* str =@"Sommetext";
for (int i =1; i < 50 ; i++) {

    str =  [[[str stringByAppendingString:@"\n"] 

    stringByAppendingString:str] stringByAppendingString:@""];

    NSLog([NSString stringWithFormat:@"%d",i]);

}

Monitoring the log shows an increase from 1 till 26 and then

crashes with the following error.

Appname(239,0xac5c9a28) malloc: * mmap(size=805306368) failed (error code=12) * error: can't allocate region

However running the same code (in java) on my android (Samsung

Galaxy S2) has no problems even when I loop it 1000 times.

UPDATE 1:

Running the code in the background using GCD returns the same error so it isn't a main thread overload problem.

UPDATE 2:

Android java code that works fine

String MsgText = "Some text";

for(i = 0 ; i < amount; i++)
        {
            ////Works on amount < 10000
            //Tested on Galaxy S2 **1GB Ram**
            MsgText = MsgText + MsgText;
        }
Basel JD
  • 275
  • 3
  • 17
  • 2
    possible duplicate of [Malloc error "can't allocate region" failed with error code 12. Any idea how to resolve this?](http://stackoverflow.com/questions/8477236/malloc-error-cant-allocate-region-failed-with-error-code-12-any-idea-how-to) – sapi Sep 19 '13 at 22:16
  • 1
    Short question (not solving this issue directly)... is there a reason you do not use an NSMutableString? :-) – Sascha Manuel Hameister Sep 19 '13 at 22:33
  • @SaschaHameister how would that help? The phone will still run out of memory. – Danilo Sep 19 '13 at 22:36
  • @Danilo Did not promised it'd help. Was just thought as an impulse to use this, because it might be better. Sure, this does not change anything regarding this issue. Hope the comment is still acceptable. (would have not opened a new answer with this comment. ;)) – Sascha Manuel Hameister Sep 19 '13 at 22:42

2 Answers2

1

After 26 rounds your str will have a length of 671.088.639 characters. It will crash because your iPhone just doesnt have enough memory in the system to allocate to store all this.

Danilo
  • 3,257
  • 2
  • 19
  • 24
  • But why does it work normally on my android device using java that has approximately the same memory (Galaxy S2) ? – Basel JD Sep 19 '13 at 22:32
  • @Danilo Must likely because the Android device is using a different internal representation for the strings or is using VM or is otherwise conserving memory (likely at the cost of CPU cycles). As well, you're going to have *a ton* of autoreleased objects with that code, unless you are running with ARC enabled and the code is optimized. – bbum Sep 19 '13 at 22:48
  • You won't be able to allocate 1.342.177.279 characters (round 27) on an Android Phone with 1 GB of memory either. You can't allocate more memory than the device physically has. – Danilo Sep 19 '13 at 22:52
  • But it successfully worked on the android device itself which made me ask this question. – Basel JD Sep 19 '13 at 22:59
  • 2
    I bet you that your java code produces different results. Check it. Running public class test { public static void main(String[] args) { String str = "Sommetext"; for (int i = 1; i < 50; i++) { str = str + "\n" + str; System.out.println(i); } } } will crash your android device (at least mine) – Danilo Sep 19 '13 at 23:01
  • 1
    @Danilo : This code works perfectly on my android! for(i = 0 ; i < amount; i++) { ////Works on amount < 10000 //Tested on Galaxy S2 **1GB Ram** MsgText = MsgText + MsgText; } – Basel JD Sep 20 '13 at 08:36
0

For the code above, the iPhone would not have enough memory to store your strdata so it will throw an allocation error and crash.

The android however, uses a different archi which might make use of the cpu so the code would work on an android although it lacks enough memory but not on an iPhone.

SamJ
  • 413
  • 1
  • 4
  • 18