
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to get memory usage at runtime using C++?
We can get the memory usage like virtual memory usage or resident set size etc. at run time. To get them we can use some system libraries. This process depends on operating systems. For this example, we are using Linux operating system.
So here we will see how to get the memory usage statistics under Linux environment using C++. We can get all of the details from “/proc/self/stat” folder. Here we are taking the virtual memory status, and the resident set size.
Example
#include <unistd.h> #include <ios> #include <iostream> #include <fstream> #include <string> using namespace std; void mem_usage(double& vm_usage, double& resident_set) { vm_usage = 0.0; resident_set = 0.0; ifstream stat_stream("/proc/self/stat",ios_base::in); //get info from proc directory //create some variables to get info string pid, comm, state, ppid, pgrp, session, tty_nr; string tpgid, flags, minflt, cminflt, majflt, cmajflt; string utime, stime, cutime, cstime, priority, nice; string O, itrealvalue, starttime; unsigned long vsize; long rss; stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt >> utime >> stime >> cutime >> cstime >> priority >> nice >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest stat_stream.close(); long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // for x86-64 is configured to use 2MB pages vm_usage = vsize / 1024.0; resident_set = rss * page_size_kb; } int main() { double vm, rss; mem_usage(vm, rss); cout << "Virtual Memory: " << vm << "\nResident set size: " << rss << endl; }
Output
Virtual Memory: 13272 Resident set size: 1548
- Related Articles
- How to get memory usage under Linux in C++
- How to optimize Python dictionary memory usage?
- How to skip TestNG test at runtime?
- How to determine the size of an Android view at runtime using Kotlin?
- How to Change the Time Interval of setinterval() Method at RunTime using JavaScript ?
- Tracking Memory Usage in PHP
- How to change a TextView's style at runtime in Android using Kotlin?
- How to install and import Python modules at runtime?
- How to remove a function at runtime in PHP?
- How to request Location Permission at runtime on iOS
- How to request Location Permission at runtime on Kotlin?
- How to change a textView Style at runtime in android?
- How to set target and action for UIBarButtonItem at runtime?
- How to see top processes sorted by actual memory usage on Linux?
- How to retrieve the test suite name at runtime in TestNG?

Advertisements