title: Why Linux VmSize VmData is larger than estimation from code?
body: <p>I want to know how many memory will take when the code is running.
  I sum all memory used in my code and use GCC to convert to an executable bin file.</p>
  
  <p>When I run the bin file, and use <code>cat/proc/$PID/status</code>, the VmSize VmData is much larger than expected. Even if remove all code but only sleep, the result still is the same,</p>
  
  <pre><code>VmPeak:    12816 kB
  VmSize:    12816 kB
  VmLck:         0 kB
  VmPin:         0 kB
  VmHWM:       964 kB
  VmRSS:       964 kB
  VmData:      204 kB
  VmStk:       136 kB
  VmExe:        56 kB
  VmLib:      2100 kB
  VmPTE:        48 kB
  VmPMD:        12 kB
  VmSwap:        0 kB
  </code></pre>
  
  <ul>
  <li>Why the memory is so large even if no data is in my code? the data I estimate in code should be 40K at most, but VmData is 204K, much larger...</li>
  <li>Why VmData is the same no matter I add #if 0 in code or not? Why not change smaller when added? At least should be 30K smaller I think...</li>
  <li>how to get memory my code is using exactly? Any other ways to test?</li>
  </ul>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  
  int main()
  {
      #if 0 
      extern int ed25519_getpub(unsigned char* public_key, const unsigned char* private_key);
      extern int ed25519_sign(unsigned char* signature,
                      const unsigned char* private_key,
                      const unsigned char* msg, const unsigned long msg_len); // use a large static global 30K array
      extern int ed25519_verify(const unsigned char* signature,
                        const unsigned char* public_key,
                        const unsigned char* msg, const unsigned long msg_len); // use the same 30K array
  
      int ret = 0;
      unsigned char public_key[32];
      unsigned char private_key[32] = "123456789ABC";
      unsigned char signature[64];
      unsigned char msg[64] = "abcdefghijklmnopqrstuvwxyz";
      sleep(20);
      #endif
  
      for (int i = 0; i &lt; 1000000; i++)
      {
          #if 0 
          //compute public key
          ret = ed25519_getpub( public_key, private_key );
          if (0 != ret)
          {
              ret = 1;
          }
  
          printf("public_key = %s, ret = %d \r\n ", public_key, ret);
  
          ret = ed25519_sign( signature, private_key, msg, strlen(msg) );
          if (0 != ret)
          {
              ret = 2;
          }
  
          printf("signature = %s, ret = %d, \r\n", signature, ret);
  
          //verify sign
          ret = ed25519_verify( signature, public_key, msg, strlen(msg) );
          if (0 != ret)
          {
              ret = 3;
          }
  
          printf("ed25519_verify ret = %d, \r\n ", ret);
          #endif
  
          if (0 == (i % 5 ))
          {
              sleep(10);
          }
      }
  
      return 0;
  }
  </code></pre>