Operating System - HP-UX
1820172 Members
4122 Online
109620 Solutions
New Discussion юеВ

Re: heap size and memory size

 
SOLVED
Go to solution
szhiyong
Frequent Advisor

heap size and memory size

Hi,

I want to use C to get heap size and memory size in the system, and how much heap and memory are used for one process.

Would someone please tell me how to do this in C?

Thanks a lot.

zhiyong
My life is now asking and learning, I wish It can change into replying and discussing
2 REPLIES 2
Animesh Chakraborty
Honored Contributor
Solution

Re: heap size and memory size

This one may help you.I got this one from this forum sometimes back.


/*
* @(#)$Header: memdetail.c,v 1.1 2001/05/03 15:35:00 nsr1521 Exp $
*
* determine detailed current memory utilized and print.
*
* No warranty is expressed or implied.
* Your mileage may vary.
*
* Contact: srobertson@bcbs-ga.com
*
*/

static char ident[] = "@(#)$Revision: 1.1 $";

#define KB 1024

#include
#include
#include

main() {
struct pst_static sbuf;
struct pst_dynamic dbuf;
struct pst_vminfo vbuf;
double page_size, sys_mem;
double phys_mem, used_phys_mem, avail_mem;
double total_virtual_mem, active_virtual_mem, avail_virtual_mem;
double total_real_mem, active_real_mem, avail_real_mem;
double total_swap_dev, used_swap_dev, avail_swap_dev;
double total_swap_mem, used_swap_mem, avail_swap_mem;

/* get static information about the system */
if (pstat_getstatic(&sbuf, sizeof(sbuf), (size_t)1, 0) != -1)
{
phys_mem = sbuf.physical_memory;
page_size = sbuf.page_size;
}
else
perror("pstat_getstatic()");

/* get dynamic information about the system */
if (pstat_getdynamic(&dbuf, sizeof(dbuf), (size_t)1, 0) != -1)
{
avail_mem = dbuf.psd_free;
total_virtual_mem = dbuf.psd_vm;
active_virtual_mem = dbuf.psd_avm;
total_real_mem = dbuf.psd_rm;
active_real_mem = dbuf.psd_arm;
}
else
perror("pstat_getdynamic()");

/* get VM information about the system */
if (pstat_getvminfo(&vbuf, sizeof(vbuf), (size_t)1, 0) != -1)
{
sys_mem = vbuf.psv_sysmem;
total_swap_dev = vbuf.psv_swapspc_max;
avail_swap_dev = vbuf.psv_swapspc_cnt;
total_swap_mem = vbuf.psv_swapmem_max;
avail_swap_mem = vbuf.psv_swapmem_cnt;
}
else
perror("pstat_getvminfo()");

/* calculate used physical memory */
used_phys_mem = phys_mem - avail_mem;
/* calculate avail virtual memory */
avail_virtual_mem = total_virtual_mem - active_virtual_mem;
/* calculate avail real memory */
avail_real_mem = total_real_mem - active_real_mem;
/* calculate used swap on device */
used_swap_dev = total_swap_dev - avail_swap_dev;
/* calculate used swap on memory */
used_swap_mem = total_swap_mem - avail_swap_mem;

(void)printf("%-15s\t%6s\t%6s\t%6s\t%6s\n",
"Memory Stat","total","used","avail","%used");

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"physical",
(phys_mem * page_size) / (KB * KB),
(used_phys_mem * page_size) / (KB * KB),
(avail_mem * page_size) / (KB * KB),
used_phys_mem * 100 / phys_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"active virtual",
(total_virtual_mem * page_size) / (KB * KB),
(active_virtual_mem * page_size) / (KB * KB),
(avail_virtual_mem * page_size) / (KB * KB),
active_virtual_mem * 100 / total_virtual_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"active real",
(total_real_mem * page_size) / (KB * KB),
(active_real_mem * page_size) / (KB * KB),
(avail_real_mem * page_size) / (KB * KB),
active_real_mem * 100 / total_real_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"memory swap",
(total_swap_mem * page_size) / (KB * KB),
(used_swap_mem * page_size) / (KB * KB),
(avail_swap_mem * page_size) / (KB * KB),
used_swap_mem * 100 / total_swap_mem);

(void)printf("%-15s\t%6.1f\t%6.1f\t%6.1f\t%5.0f%%\n",
"device swap",
(total_swap_dev * page_size) / (KB * KB),
(used_swap_dev * page_size) / (KB * KB),
(avail_swap_dev * page_size) / (KB * KB),
used_swap_dev * 100 / total_swap_dev);

return;
}





Did you take a backup?
szhiyong
Frequent Advisor

Re: heap size and memory size

Hi,Animesh,

Thanks a lot for help!

This program help me a lot.

zhiyong
My life is now asking and learning, I wish It can change into replying and discussing