# Obtaining Total and Resident Virtual Memory



## mdannenberg (Jun 8, 2012)

I am writing a program in which I need to show the user the total virtual memory and the current resident virtual memory for the program itself.

As far as the total virtual memory sysctl seems to do what I need, but it's only working on the 64bit machine I am testing on, not the 32bit one (both running 8.3). On the 32bit machine it returns -1 for total virtual memory. What is happening? Should I be using something other than sysctl?

With regards to resident virtual memory is there a good way to obtain it? I know on OS X, one gets it by calling task_for_pid and extracting it from the task_basic_info they by calling task_info. Is there a similar call for *F*reeBSD?

Thanks in advance.

Dannenberg


----------



## expl (Jun 8, 2012)

What sysctl key are you using? You should be reading vm.stats.vm.v_page_count that returns VM size in pages. To get page size use getpagesize(3).

For process used memory you want to use kvm_getprocs(),  memory used is located within the kinfo_proc structure at .eproc.e_vm.vm_rssize. Check vm/vm.h and sys/kinfo_proc.h for reference.


----------



## mdannenberg (Jun 11, 2012)

Not quite sure what you mean by sysctl key. Is that for sysctlbyname? If so, can you point me to an example of sysctlbyname and a list of what you can pass as a sysctl key?

As far as kinfo_proc, the struct seems to vary dramatically. I could find places online where it matches what you outlined for me, but on one of the machines I am using it matches this: http://bintree.net/freebsd/d8/ddb/user_8h_source.html. The two different versions seem to make this not portable. Is there a good way to deal with this? Or maybe a method that wouldn't depend on this struct?

Thanks again.
Dannenberg


----------



## expl (Jun 11, 2012)

mdannenberg said:
			
		

> Not quite sure what you mean by sysctl key. Is that for sysctlbyname? If so, can you point me to an example of sysctlbyname and a list of what you can pass as a sysctl key?



Check this small lib I have written for example: http://code.google.com/p/sysinfo-bsd/source/browse/sysinfo.c
Particularly where I get total VM size (lines 129 and 140). 



			
				mdannenberg said:
			
		

> As far as kinfo_proc, the struct seems to vary dramatically. I could find places online where it matches what you outlined for me, but on one of the machines I am using it matches this: http://bintree.net/freebsd/d8/ddb/user_8h_source.html. The two different versions seem to make this not portable. Is there a good way to deal with this? Or maybe a method that wouldn't depend on this struct?
> 
> Thanks again.
> Dannenberg



It's my bad, I looked at kernel header not the userworld one... the resident size is under (segsz_t)kinfo_proc.ki_rssize (size in pages).


----------

