<p>Under Linux it's possible to read the control file <code>memory.usage_in_bytes</code> <a href="https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt" rel="nofollow noreferrer">for Control Group v1</a> or <code>memory.current</code> for <a href="https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html" rel="nofollow noreferrer">Control Group v2</a>. How to get the memory usage of a <a href="https://learn.microsoft.com/en-us/virtualization/windowscontainers/about/" rel="nofollow noreferrer">Windows container</a>?</p>
<p>According to the <a href="https://kubernetes.io/docs/concepts/configuration/windows-resource-management/" rel="nofollow noreferrer">Windows resource management</a> Kubernetes docs, the Windows concept for the process isolation is about <a href="https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects" rel="nofollow noreferrer">job objects</a>. I found that it's possible to get <a href="https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_extended_limit_information" rel="nofollow noreferrer">JOBOBJECT_EXTENDED_LIMIT_INFORMATION</a> information providing <code>PeakJobMemoryUsed</code>. However, it appears querying this information requires elevated rights (because I got <code>Could not get job information: [0x0005] Access is denied.</code>).</p>
<p>Is there any other way to get information about Windows containers memory usage?</p>

Christian Ammer
<blockquote>
<p>I would like to determine the memory usage from a C++ program</p>
</blockquote>
<p>To expand on <a href="https://stackoverflow.com/users/15511041/yangxiaopo-msft">YangXiaoPo-MSFT</a>'s <a href="https://stackoverflow.com/questions/76849219/how-to-retrieve-the-memory-usage-of-a-windows-container#comment135506300_76849219">comment</a>, I just tried on my PC (outside a Kubernetes setting though) to retrieve these metrics programmatically from C++ using the <a href="https://learn.microsoft.com/en-us/windows/win32/perfctrs/using-the-pdh-functions-to-consume-counter-data" rel="noreferrer">Performance Data Helper (PDH) library</a>:</p>
<p>I added <code>pdh.lib</code> to my linker input, and use it with:</p>
<p><strong>GPUProcessMemoryQuery.cpp</strong>:</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;windows.h&gt;
#include &lt;pdh.h&gt;
#include &lt;pdhmsg.h&gt;
#include &lt;iostream&gt;

int main()
{
    PDH_HQUERY hQuery = NULL;
    PDH_HCOUNTER hCounter = NULL;
    PDH_STATUS pdhStatus = PdhOpenQuery(NULL, NULL, &amp;hQuery);

    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cerr &lt;&lt; &quot;Failed to open PDH query.\n&quot;;
        return 1;
    }

    pdhStatus = PdhAddCounter(hQuery, L&quot;\\GPU Process Memory(*)\\Dedicated Usage&quot;, 0, &amp;hCounter);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cerr &lt;&lt; &quot;Failed to add PDH counter.\n&quot;;
        return 1;
    }

    pdhStatus = PdhCollectQueryData(hQuery);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cerr &lt;&lt; &quot;Failed to collect PDH query data.\n&quot;;
        return 1;
    }

    PDH_FMT_COUNTERVALUE counterValue;
    pdhStatus = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG, NULL, &amp;counterValue);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cerr &lt;&lt; &quot;Failed to get formatted counter value.\n&quot;;
        return 1;
    }

    std::wcout &lt;&lt; L&quot;GPU Process Memory (Dedicated Usage): &quot; &lt;&lt; counterValue.longValue &lt;&lt; L&quot;\n&quot;;

    PdhCloseQuery(hQuery);
    return 0;
}
</code></pre>
<p><a href="https://i.stack.imgur.com/E3GWD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/E3GWD.png" alt="Visual Studio" /></a></p>
<p>I got: <code>GPU Process Memory (Dedicated Usage): 35549184</code>, without requiring  elevated rights.</p>
<hr />
<p>In your case, measure the memory usage of a specific Windows container using the Performance Counters on Windows is a bit more involved.</p>
<p>You need first to determine the appropriate performance counter: Windows containers' performance metrics should be under namespaces like <code>Hyper-V Container</code> or similar (this may change depending on the Windows Server version and container runtime).</p>
<p>Before diving into C++, you might want to use <code>Performance Monitor</code> (type <a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/perfmon" rel="noreferrer"><code>perfmon</code></a> in the Windows start menu) to browse available counters related to containers. That will give you an idea of what is available and the exact path you will need to use in your code.</p>
<p>Once you have the right counter path:</p>
<pre class="lang-cpp prettyprint-override"><code>// Change this line:
pdhStatus = PdhAddCounter(hQuery, L&quot;\\GPU Process Memory(*)\\Dedicated Usage&quot;, 0, &amp;hCounter);

// To something like:
pdhStatus = PdhAddCounter(hQuery, L&quot;\\Hyper-V Container\\Memory Metric (or appropriate counter)&quot;, 0, &amp;hCounter);
</code></pre>
<p>It is crucial to identify which instance of the counter you are interested in, since each container should have its own instance. In the Performance Monitor, you can see instances of each counter (like individual container names or IDs). Replace the <code>*</code> in the counter path with the exact instance name to monitor a specific container.</p>
