Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Memory (http://www.programmingforums.org/showthread.php?t=1353)

extractor Nov 30th, 2004 6:45 AM

Is there a function in c++ to measure memory use of a program?

Dia_Byte Nov 30th, 2004 9:39 AM

a C++ function ... mmmmmm...

i don't know

but i'm sure that you can use some API's and types to get the memory

i don't remember how i found a VB program a while ago which uses APIs to get the memory

try checking MSDN maybe you'lle find anything usefull

Eggbert Nov 30th, 2004 11:02 AM

>Is there a function in c++ to measure memory use of a program?
Not a standard function. You would need to check your system's documentation to see if the system API supports such a thing.

Dia_Byte Dec 2nd, 2004 11:34 AM

there is

look what

i will look around my VB codes and when i find something i'll post it here

Dia_Byte Dec 3rd, 2004 8:40 AM

this is the source code in visual basic :

:

Option Explicit

Private Type MEMORYSTATUS        ' the type to get memory status
 dwLength As Long
 dwMemoryLoad As Long
 dwTotalPhys As Long
 dwAvailPhys As Long
 dwTotalPageFile As Long
 dwAvailPageFile As Long
 dwTotalVirtual As Long
 dwAvailVirtual As Long
End Type

Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)        'the API for memory

Const fmt As String = "###,###,###,###"
Const skb As String = " Kbyte"
Const nkb As Long = 1024

Private Sub Form_Load()

Dim MS As MEMORYSTATUS 'declare MS as new type of MEMORYSTATUS

MS.dwLength = Len(MS)

GlobalMemoryStatus MS ' use the API

lbMemStat(0) = Format$(MS.dwMemoryLoad, fmt) & " % used" 'get used memory
lbMemStat(1) = Format$(MS.dwTotalPhys / nkb, fmt) & skb        'get total physical memory
lbMemStat(2) = Format$(MS.dwAvailPhys / nkb, fmt) & skb        'get available physical Memory
lbMemStat(3) = Format$(MS.dwTotalPageFile / nkb, fmt) & skb
lbMemStat(4) = Format$(MS.dwAvailPageFile / nkb, fmt) & skb
lbMemStat(5) = Format$(MS.dwTotalVirtual / nkb, fmt) & skb 'get total virtual memory
lbMemStat(6) = Format$(MS.dwAvailVirtual / nkb, fmt) & skb 'get avialable virual memory

End Sub



and this is the program in C++ as you ordered written by me :

:

#include <iostream.h>        // for the 'cout' function
#include <stdlib.h> // for the 'system' function
#include <windows.h>        // for Windows APIs [don't remove !!]
#include <winuser.h>        // for Windows APIs too        [don't remove !!]

void decs();

int main()
{
 _MEMORYSTATUS MS;        // declare memory structure MEMORYSTATUS stored in winbase.h
 
 decs();
 
 cout << endl << "Start Of Memory Test ..." << endl << endl;
 
 cout << "Current Memory Utilization : " << MS.dwMemoryLoad << endl;
 cout << "Total Memory : " << MS.dwTotalPhys << " Bytes" << endl;
 cout << "Available Physical Memory : " << MS.dwAvailPhys << " Bytes" << endl;
 cout << "Total Page File : " << MS.dwTotalPageFile << endl;
 cout << "Available Page File : " << MS.dwAvailPageFile << " Bytes" << endl;
 cout << "Total Virtual Memory : " << MS.dwTotalVirtual << " Bytes" << endl;
 cout << "Avialable Virtual Memory : " << MS.dwAvailVirtual << " Bytes" << endl << endl;
 
 cout << "End of memory test ..." << endl << endl;
 
 system("pause"); // the console command 'pause'
 
 return 0;
}

void decs()
{
  cout << "************************************" << endl;
  cout << "Memory Status Program" << endl;
  cout << "By Dia_Byte" << endl;
  cout << "************************************" << endl;
}


you can compile the source code with DEV_CPP


All times are GMT -5. The time now is 2:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC