2

When I create an 8MB NSMutableData object my memory usage does not seem to change. Is it normal?

My code is as following:

NSMutableData  *myData;
myData = [[NSMutableData alloc] initWithLength:(8*1024*1024)]; // init with 8 MB

When I debug step by step, the memory usage stays at 5.8 MB, even after the object has been allocated... It should go up to 13.8, shouldn't it? I am using ARC (but with MRR it is the same...).

If I try to access the data, memory usage remains the same:

uint8_t *fBuffer;
fBuffer = (uint8_t *)malloc(8*1024*1024);
[myData getBytes:fBuffer length:8*1024*1024];

In fact, if I modify the data, then the memory usage goes up as it should!

for (int i= 0 ; i<8000000 ; i++) {
    fBuffer[i] = 0x32;
}

=> memory usage is now 13.4 MB.

Thank you all for your help, I now understand how it is supposed to work.

Dertron
  • 141
  • 1
  • 8
  • Does it go up if you access the data's bytes? – jtbandes Oct 10 '15 at 19:11
  • Related: [How does NSMutableData allocate memory?](http://stackoverflow.com/q/3387090) – jscs Oct 10 '15 at 19:23
  • Thank you @Josh Caswell, regarding the link you gave, it seems that it is not because you allocate memory with initWithLength that the memory will really be used. But if you modify the data, then the memory allocation usage change. – Dertron Oct 10 '15 at 19:39

0 Answers0