title: How is memory handled once touched for the first time in numpy.zeros?
body: <p>I recently saw that when creating a numpy array via <code>np.empty</code> or <code>np.zeros</code>, the memory of that numpy array is not actually allocated by the operating system as discussed in <a href="https://stackoverflow.com/a/44487972/12769783">this answer</a> (and <a href="https://stackoverflow.com/questions/51314255/why-doesnt-numpy-zeros-allocate-all-of-its-memory-on-creation-and-how-can-i-fo">this question</a>), because numpy utilizes <code>calloc</code> to allocate the array's memory.</p>
  <blockquote>
  <p>In fact, the OS isn't even &quot;really&quot; allocating that memory until you try to access it.</p>
  </blockquote>
  <p>Therefore,</p>
  <pre class="lang-py prettyprint-override"><code>l = np.zeros(2**28)
  </code></pre>
  <p>does not increase the utilized memory the system reports, e.g., in <code>htop</code>.
  Only once I touch the memory, for instance by executing</p>
  <pre class="lang-py prettyprint-override"><code>np.add(l, 0, out=l)
  </code></pre>
  <p>the utilized memory is increased.</p>
  <p>Because of that behaviour I have got a couple of questions:</p>
  <h3>1. Is touched memory copied under the hood?</h3>
  <p>If I touch chunks of the memory only after a while, is the content of the numpy array copied under the hood by the operating system to guarantee that the memory is contiguous?</p>
  <pre class="lang-py prettyprint-override"><code>i = 100
  f[:i] = 3
  
  while True:
      ... # Do stuff
      f[i] = ... # Once the memory &quot;behind&quot; the already allocated chunk of memory is filled
                  # with other stuff, does the operating system reallocate the memory and
                  # copy the already filled part of the array to the new location?
      i = i + 1
  </code></pre>
  <h3>2. Touching the last element</h3>
  <p>As the memory of the numpy array is continguous in memory, I tought</p>
  <pre class="lang-py prettyprint-override"><code>f[-1] = 3
  </code></pre>
  <p>might require the enitre block of memory to be allocated (without touching the entire memory).
  However, it does not, the utilized memory in htop does not increase by the size of the array.
  Why is that not the case?</p>