Get the memory usage of all running processes.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jurjen's picture

There is an API function

There is an API function named GetProcessMemoryInfo which does what you ask. There is no 4GL example here at oehive, but you can find the API documentation for the function at msdn.microsoft.com.


Thanks

Thanks thats what I needed. Function worked perfect.


tamhas's picture

So, if you now have 4GL code

So, if you now have 4GL code for the purpose, you might post it for the next seeker.


Code to get Process Memory Usage

&GLOB TH32CS_SNAPPROCESS 2
&GLOB PROCESS_QUERY_INFORMATION 1024
&GLOB PROCESS_VM_READ 16
&GLOB MAX_PATH 260

/*Procedures to get PID and Process name*/
PROCEDURE CreateToolhelp32Snapshot EXTERNAL "kernel32" :
DEFINE INPUT PARAMETER dwFlags AS LONG.
DEFINE INPUT PARAMETER th32ProcessId AS LONG.
DEFINE RETURN PARAMETER hSnapShot AS LONG.
END PROCEDURE.

PROCEDURE Process32First EXTERNAL "kernel32" :
DEFINE INPUT PARAMETER hSnapShot AS LONG.
DEFINE INPUT PARAMETER lpProcessEntry32 AS MEMPTR.
DEFINE RETURN PARAMETER ReturnValue AS LONG.
END PROCEDURE.

PROCEDURE Process32Next EXTERNAL "kernel32" :
DEFINE INPUT PARAMETER hSnapShot AS LONG.
DEFINE INPUT PARAMETER lpProcessEntry32 AS MEMPTR.
DEFINE RETURN PARAMETER ReturnValue AS LONG.
END PROCEDURE.

/*Procedures to get Memory Usage*/
PROCEDURE GetProcessMemoryInfo EXTERNAL "PSAPI.DLL":
DEFINE INPUT PARAMETER hProcess AS LONG.
DEFINE INPUT PARAMETER ppsmemCounters AS LONG.
DEFINE INPUT PARAMETER cb AS LONG.
DEFINE RETURN PARAMETER ReturnValue AS LONG.
END PROCEDURE.

PROCEDURE OpenProcess EXTERNAL "kernel32.dll" :
DEFINE INPUT PARAMETER dwDesiredAccess AS LONG.
DEFINE INPUT PARAMETER bInheritHandle AS LONG.
DEFINE INPUT PARAMETER dwProcessId AS LONG.
DEFINE RETURN PARAMETER hProcess AS LONG.
END PROCEDURE.

PROCEDURE CloseHandle EXTERNAL "kernel32" :
DEFINE INPUT PARAMETER hObject AS LONG.
DEFINE RETURN PARAMETER ReturnValue AS LONG.
END PROCEDURE.

PROCEDURE GetProcessInfo:

DEFINE VARIABLE hSnapShot AS INTEGER NO-UNDO.
DEFINE VARIABLE lpPE AS MEMPTR NO-UNDO.
DEFINE VARIABLE ReturnValue AS INTEGER NO-UNDO.
DEFINE VARIABLE list AS CHARACTER NO-UNDO.
DEFINE VARIABLE LviMem_Usage AS INTEGER NO-UNDO.
DEFINE VARIABLE LviPid AS INTEGER NO-UNDO.

/* Create and open SnapShot-list */
RUN CreateToolhelp32Snapshot({&TH32CS_SNAPPROCESS},
0,
OUTPUT hSnapShot).
IF hSnapShot = -1 THEN RETURN.

/* init buffer for lpPE */
SET-SIZE(lpPE) = 336.
PUT-LONG(lpPE, 1) = GET-SIZE(lpPE).

/* Cycle thru process-records */
RUN Process32First(hSnapShot,
lpPE,
OUTPUT ReturnValue).
DO WHILE ReturnValue NE 0:

LviPid = INTEGER(GET-LONG(lpPE, 9)).

RUN GetProcessMemory in THIS-PROCEDURE (LviPid ,OUTPUT LviMem_Usage) .

ASSIGN list = list + "~n"
list = list + STRING(GET-LONG(lpPE, 9)) + "|" /* Add PID to list */
list = list + GET-STRING(lpPE, 37) + "|" /* Add process name to list */
list = list + TRIM(STRING(LviMem_Usage / 1024,">>>,>>>,>>>,>>>,>>9")) + " kb". /* Add Memory Usage to list */

RUN Process32Next(hSnapShot,
lpPE,
OUTPUT ReturnValue).
END.

/* Close SnapShot-list */
RUN CloseHandle(hSnapShot, OUTPUT ReturnValue).

MESSAGE list VIEW-AS ALERT-BOX INFO BUTTONS OK.

END PROCEDURE.

PROCEDURE GetProcessMemory :

DEFINE INPUT PARAMETER LviPid AS INTEGER NO-UNDO .
DEFINE OUTPUT PARAMETER LviMemory_Size AS INTEGER NO-UNDO .

DEFINE VARIABLE process_memory_counter_struct AS MEMPTR NO-UNDO.
DEFINE VARIABLE hProcess AS INTEGER NO-UNDO.
DEFINE VARIABLE returnvalue AS INTEGER NO-UNDO.

RUN OpenProcess ( {&PROCESS_QUERY_INFORMATION} + {&PROCESS_VM_READ},
0,
LviPid,
OUTPUT hProcess).

&SCOPED-DEFINE offset_WorkingSetSize 13

IF hProcess <> 0 THEN DO:
ASSIGN SET-SIZE(process_memory_counter_struct) = 40.

RUN GetProcessMemoryInfo (INPUT hProcess,
INPUT GET-POINTER-VALUE(process_memory_counter_struct),
INPUT GET-SIZE(process_memory_counter_struct),
OUTPUT returnvalue).

ASSIGN LviMemory_Size = GET-LONG(process_memory_counter_struct,
{&offset_WorkingSetSize})
set-size(process_memory_counter_struct) = 0.

RUN CloseHandle (hProcess,
OUTPUT ReturnValue).
END.

END PROCEDURE .

RUN GetProcessInfo.


tamhas's picture

The answer is _very_ OS

The answer is _very_ OS dependent ... and probably has some tricky bits even then, since much of the memory footprint of a client is shared.


Thanks for the response

Thanks for the response, I guess I could have explained it a little better. The program will only be running on one machine with OS Windows 2000. Also when I say I am looking for memory usage from all processes basically I want for each process in the task manager is the memory usage that is displayed in the task manager. I am able to get the PID and the image name of each process from the example at www.oehive.org/node/508 but I cant seem to figure out a way to get the memory usage.