Posts

Showing posts with the label FreeBSD

Measuring Physical and Virtual Memory Usage in FreeBSD

Image
Terminal — memory-usage on FreeBSD $ clang++ memory-usage.cpp -o memory-usage $ ./memory-usage 839 Process ID: 839 Physical memory used: 2228.00 KB Virtual memory used: 12844.00 KB Here is a C++ program to print the physical and virtual memory used by a process on FreeBSD. It uses the kernel's sysctl interface to read process information, so it does not depend on parsing the output of any command line tool. What this program does It includes the necessary headers for system calls and I/O operations. It uses std::atoi() for argument parsing and <iomanip> for formatting output. The main function handles command line arguments: If an argument is provided, it is converted to an integer with std::atoi() . If the converted PID is invalid (0 or negative), an error message is displayed. If no argument is provided, it uses getpid() to get the current process ID. It sets up the Management Information Base (MIB) for the sysctl call to retrieve process ...