title: Non voluntary context switches: How can I prevent them?
body: <p><br />
  I have a small application that I was running right now and I wanted to check if I have any memory leaks in it so I put in this piece of code:</p>
  
  <pre><code>for (unsigned int i = 0; i&lt;10000; i++) {
      for (unsigned int j = 0; j&lt;10000; j++) {
          std::ifstream &amp;a = s-&gt;fhandle-&gt;open("test");
          char temp[30];
          a.getline(temp, 30);
          s-&gt;fhandle-&gt;close("test");
      }
  }
  </code></pre>
  
  <p>When I ran the application i cat'ed /proc//status to see if the memory increases.
  The output is the following after about 2 Minutes of runtime:</p>
  
  <pre><code>Name:   origin-test
  State:  R (running)
  Tgid:   7267
  Pid:    7267
  PPid:   6619
  TracerPid:  0
  Uid:    1000    1000    1000    1000
  Gid:    1000    1000    1000    1000
  FDSize: 256
  Groups: 4 20 24 46 110 111 119 122 1000 
  VmPeak:   183848 kB
  VmSize:   118308 kB
  VmLck:         0 kB
  VmHWM:      5116 kB
  VmRSS:      5116 kB
  VmData:     9560 kB
  VmStk:       136 kB
  VmExe:        28 kB
  VmLib:     11496 kB
  VmPTE:       240 kB
  VmSwap:        0 kB
  Threads:    2
  SigQ:   0/16382
  SigPnd: 0000000000000000
  ShdPnd: 0000000000000000
  SigBlk: 0000000000000000
  SigIgn: 0000000000002004
  SigCgt: 00000001800044c2
  CapInh: 0000000000000000
  CapPrm: 0000000000000000
  CapEff: 0000000000000000
  CapBnd: ffffffffffffffff
  Cpus_allowed:   3f
  Cpus_allowed_list:  0-5
  Mems_allowed:   00000000,00000001
  Mems_allowed_list:  0
  voluntary_ctxt_switches:    120
  nonvoluntary_ctxt_switches: 26475
  </code></pre>
  
  <p>None of the values did change except the last one, so does the mean there are no memory leaks?</p>
  
  <p>But what's more important and what I would like to know is, if it is bad that the last value is increasing rapidly (about 26475 switches in about 2 Minutes!).</p>
  
  <p>I looked at some other applications to compare how much non-volunary switches they have:</p>
  
  <ol>
  <li>Firefox: about 200</li>
  <li>Gdm: 2</li>
  <li>Netbeans: 19</li>
  </ol>
  
  <p>Then I googled and found out some stuff but it's to technical for me to understand.
  What I got from it is that this happens when the application switches the processor or something? (I have an Amd 6-core processor btw).</p>
  
  <p>How can I prevent my application from doing that and in how far could this be a problem when running the application?</p>
  
  <p>Thanks in advance,
  Robin.</p>