Radek ChalupaČlánkyUkázky kóduTipy a trikyAktualityŠkolení a konzultaceVývoj softwareFreewareKontakt

Jak v Linuxu získat některé informace o využití paměti.

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/sysinfo.h>

int main(int argc, const char** argv)
{
	size_t phys_pages = (size_t)sysconf(_SC_PHYS_PAGES);
	size_t page_size = (size_t)sysconf(_SC_PAGE_SIZE);
	size_t av_phys_pages = (size_t)sysconf(_SC_AVPHYS_PAGES);
	printf("phys. pages: %lu\n", (unsigned long)phys_pages);
	printf("page size: %lu\n", (unsigned long)page_size);
	printf("av phys. pages: %lu\n\n", (unsigned long)av_phys_pages);
	struct sysinfo sinfo;
	sysinfo(&sinfo);
	printf("celkem RAM: %llu\n", (unsigned long long)sinfo.totalram);
	printf("volná RAM: %llu\n", (unsigned long long)sinfo.freeram);
	printf("velikost swapu: %llu\n", (unsigned long long)sinfo.totalswap);
	printf("volný swap: %llu\n", (unsigned long long)sinfo.freeswap);
	printf("hotovo ...\n");
	getchar();
	return EXIT_SUCCESS;
}

Kód lze sestavit následujícím příkazem:

g++ info-pamet-linux.cpp -Wall -oinfo-pamet-linux

Copyright © 2019 - Radek Chalupa