title: Actual used memory for mqueue
body: <p>I'm trying to find a command to get the actual reserved memory for <code>mqueue</code>.</p>
  
  <p>In <code>/proc/self/limits</code> is stored limit of total size for <code>mqueue</code> (in my case 819200). </p>
  
  <p>It means that if I have 10 <code>mqueues</code> with a limit of 10 msg and size 8192, then total size is <code>10*10*8192=819200</code> which is number in system limit.</p>
  
  <p>I know how to increase this limit but I don't know how to get the actual used memory for <code>mqueue</code> (for example if I'm currently using 6 <code>mqueues</code>).</p>
  
  <ul>
  <li>if I cretate 6 mqueues with settings 10 msg and size 8192,</li>
  <li>then alocated memory will be 6*10*8192 = 491520</li>
  <li><p>and my question is where I can found this size 491520</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;cerrno&gt;
  #include &lt;cstring&gt;
  #include &lt;stdexcept&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include "mqueue.h"
  
  std::string exec(const char* cmd) {
      char buffer[128];
      std::string result = "";
      FILE* pipe = popen(cmd, "r");
      if (!pipe) throw std::runtime_error("popen() failed!");
      try {
          while (!feof(pipe)) {
              if (fgets(buffer, 128, pipe) != NULL)
                  result += buffer;
          }
      } catch (...) {
          pclose(pipe);
          throw;
      }
      pclose(pipe);
      return result;
  }
  
  class mqueue {
  public:
      mqueue(std::string n = "/dummy", int maxMsg = 10) : queue(), attr(), name(n) {
      /* Set attributes */
      this-&gt;attr.mq_flags = 0;
      this-&gt;attr.mq_maxmsg =  maxMsg; /* war: MAX_MSG_QUEUE */
      this-&gt;attr.mq_msgsize = 8192; /* war: MAX_MSG_SIZE */
  
      /* Destroy old message queue */
      mq_unlink(this-&gt;name.c_str());
  
      /* Open message queue */
      queue = mq_open(this-&gt;name.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, &amp;this-&gt;attr);
      std::cout &lt;&lt; "\n[info] in constructor class mqueue name: " &lt;&lt; this-&gt;name &lt;&lt; " | errno: " &lt;&lt; std::strerror(errno);
     }
      ~ mqueue() {
          //std::cout &lt;&lt; "\n[info] in destructor class mqueue name: " &lt;&lt; this-&gt;name &lt;&lt; " | close = " &lt;&lt; mq_close(this-&gt;queue) &lt;&lt; " | unlink = " &lt;&lt; mq_unlink(this-&gt;name.c_str());
      }
      void getInfo() const {
          std::cout &lt;&lt; "\nname: " &lt;&lt; this-&gt;name &lt;&lt; " | mqueue: " &lt;&lt; this-&gt;queue &lt;&lt; "\n" ;
      }
  private:
      std::string name;
      mqd_t queue;
      mq_attr attr;
  };
  
  int main(int argc, char **argv)
  {
      int i;
      mqueue testMqueue("/dummy");
      testMqueue.getInfo();
  
      mqueue testMqueue1("/dummy1", 1);
      mqueue testMqueue2("/dummy2", 6);
      mqueue testMqueue3("/dummy3", 7);
      mqueue testMqueue4("/dummy4", 10);
      mqueue testMqueue5("/dummy5", 2);
      mqueue testMqueue6("/dummy6", 3);
  
      mqd_t mqdes;
      mq_attr mqstat;
      for (i = 0; i &lt; 99; i++) {
          mqdes = i;
          mq_getattr(mqdes, &amp;mqstat);
          std::cout &lt;&lt; "\n" &lt;&lt; i &lt;&lt; "\tmaxmsg: " &lt;&lt; mqstat.mq_maxmsg &lt;&lt; "\tmsgsize: " &lt;&lt; mqstat.mq_msgsize &lt;&lt; "\tsize: " &lt;&lt; mqstat.mq_maxmsg * mqstat.mq_msgsize;
      }
      return 0;
  }
  </code></pre></li>
  </ul>
  
  <p>Thanks for answers.
  Adrian</p>
  
  <p>Example:</p>
  
  <p>Compile and execute:</p>
  
  <pre><code>std::vector&lt;mqueue&gt; testMqueue;
  for (i = 0; i &lt; 20; i++) {
      testMqueue.push_back(mqueue("/dummy" + std::to_string(i)));
      testMqueue.at(i).getInfo(); 
  }
  std::cout &lt;&lt; exec("ipcs -q") ;
  </code></pre>
  
  <p>and as you can see:</p>
  
  <pre><code>[info] in constructor class mqueue name: /dummy19 | errno: Too many open files
   name: /dummy19 | mqueue: -1
  
   ------ Message Queues --------
   key        msqid      owner      perms      used-bytes   messages    
  </code></pre>
  
  <p><strong>list is empty also when I executing ipcs -q in terminal</strong></p>