Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some code I wrote to try to do this in a portable way. It's not perfect, but I think it should at least give a pointer to how to do this on each of several platforms.</p> <p>(P.S. I use OSX and Linux regularly, and know this works well. I use Windows more rarely, so caveats apply to the Windows clause, but I think it's right.)</p> <pre><code>#ifdef __linux__ # include &lt;sys/sysinfo.h&gt; #endif #ifdef __APPLE__ # include &lt;mach/task.h&gt; # include &lt;mach/mach_init.h&gt; #endif #ifdef _WINDOWS # include &lt;windows.h&gt; #else # include &lt;sys/resource.h&gt; #endif /// The amount of memory currently being used by this process, in bytes. /// By default, returns the full virtual arena, but if resident=true, /// it will report just the resident set in RAM (if supported on that OS). size_t memory_used (bool resident=false) { #if defined(__linux__) // Ugh, getrusage doesn't work well on Linux. Try grabbing info // directly from the /proc pseudo-filesystem. Reading from // /proc/self/statm gives info on your own process, as one line of // numbers that are: virtual mem program size, resident set size, // shared pages, text/code, data/stack, library, dirty pages. The // mem sizes should all be multiplied by the page size. size_t size = 0; FILE *file = fopen("/proc/self/statm", "r"); if (file) { unsigned long vm = 0; fscanf (file, "%ul", &amp;vm); // Just need the first num: vm size fclose (file); size = (size_t)vm * getpagesize(); } return size; #elif defined(__APPLE__) // Inspired by: // http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&amp;t_info, &amp;t_info_count); size_t size = (resident ? t_info.resident_size : t_info.virtual_size); return size; #elif defined(_WINDOWS) // According to MSDN... PROCESS_MEMORY_COUNTERS counters; if (GetProcessMemoryInfo (GetCurrentProcess(), &amp;counters, sizeof (counters))) return counters.PagefileUsage; else return 0; #else // No idea what platform this is return 0; // Punt #endif } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload