title: allocate specific amount of memory in swift - iOS
body: <p>I am learning Instruments so I created a simple app that lets the user input a number of Mega Bytes (MB) to be allocated. And I use the "Allocations" tool in Instruments to make sure this exact amount of data is allocated. However, I always see more memory allocated in Instruments than I expect.</p>
  
  <p>I did the following</p>
  
  <pre><code>func allocateMemoryOfSize(numberOfBytes: Int) {
  
      var bytesArray = [Int8]()
      for i in 0 ..&lt; numberOfBytes {
          bytesArray.append(Int8(i % 7))
      }
  }
  </code></pre>
  
  <p>I checked many questions and I found an alternative using:</p>
  
  <pre><code>func allocateMemoryOfSize(numberOfBytes: Int) {
        var imageData = UnsafeMutablePointer&lt;Int8&gt;.alloc(numberOfBytes)}
  </code></pre>
  
  <p>But I still get more memory allocated. Here is the restults in Instruments when I allocate 10 MBs:</p>
  
  <p>Before running the allocateMemoryOfSize function:</p>
  
  <p><a href="https://i.stack.imgur.com/oCBC7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oCBC7.png" alt="Before: only 20.58 MB used"></a></p>
  
  <p>During the running of the function: 44.96 MB used
  <a href="https://i.stack.imgur.com/QuKlN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QuKlN.png" alt="During: 44.96 MB used"></a></p>
  
  <p>After the running of the function: back to only 20.58 MB
  <a href="https://i.stack.imgur.com/1QXTg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1QXTg.png" alt="After: back to only 20.58 MB"></a></p>
  
  <p>So, the function allocates around 22 MBs not 10 as wanted. What am I doing wrong? or is this extra memory management stuff? </p>