Read memory usage directly from taskmanager?

BlitzMax Forums/BlitzMax Programming/Read memory usage directly from taskmanager?

Grisu(Posted 2007) [#1]
Hello everyone!

Is it somehow possible to read out the amount of memory usage of a program directly from the taskmanger?



Means:
I no longer have to open the taskmanager myself and watch the vaules. Instead my program reads all the informations and I can output them "inside" the app itself.

Grisu


degac(Posted 2007) [#2]
GCMemAlloced() doen't report all the memory used by the application? Or am I wrong?


Dreamora(Posted 2007) [#3]
No, the memory returned has actually not much to do with the really used memory.


Grisu(Posted 2007) [#4]
^^ Exactly my problem.

In addition GCMemAlloced() gives me strange results such as a memory usage of over 1 GB. So I'm looking for a workaround as GCCollector is not 100% bullet proofed.


Warren(Posted 2007) [#5]
You're saying the garbage collector isn't working? You should post a code sample to the bug board showing this then. That's kind of serious.


Grisu(Posted 2007) [#6]
If I had an easy to use example code, I would post it.

Though the garbage collector is freeing the allocated memory as and when he wants to. So it's hard to spot memory leakes.
It's much easier imho to use the taskmanager to track the amount of system memory used by an app. Therefore I'm looking for this alternative.


grable(Posted 2007) [#7]
The task manager isnt entirely accurate itself ;)


degac(Posted 2007) [#8]
Digging on MSDN I found what I think should get the info

GetProcessWorkingSetSize
http://msdn2.microsoft.com/en-us/library/ms683226.aspx

but it seems that a process has a min and a max of memory space, so the info is not accurate...

Then how make it to work with Bmax is another story...too much structures to define (and understand before!)
Byez


grable(Posted 2007) [#9]
You could try shoehorning in a leak detector,
or roll your own by replacing malloc/free and overriding c++ new/delete.

But im not sure how max does its type allocation, eg wheter the GC has its own memory pool or uses the process heap.

Good luck =) hehe


Dreamora(Posted 2007) [#10]
The GC is accurate in returning what it does.
But what it returns is not its current full memory usage on the OS. It is the memory it pools.

The difference comes here when you use anything outside the GC: MaxGUI, OGG, MemAlloc, Byte Ptr, CString ...


Fabian.(Posted 2007) [#11]
Sadly, I can't get this working on Linux or MacOS, since I don't know much about the APIs of that systems, however, since you asked about the Task-Manager, I assume that a Win32 only solution would be enough. Note that this code needs the Psapi.dll, I don't know whether this is always included into the system, but at least every XP Home and Pro should have this, since (I guess) the Task-Mamager uses the same dll:
Strict
Framework brl.blitz

Local Lib = LoadLibraryW ( "Psapi.dll".ToWString ( ) )
If Not Lib
  WriteStdout "Psapi.dll is missing, make sure that you've got a valid Windows XP installation~n"
  Return
EndIf
Global GetProcessMemoryInfo ( Process , MemCounters:Byte Ptr , Size ) = GetProcAddress ( Lib , "GetProcessMemoryInfo".ToCString ( ) )
If Not GetProcessMemoryInfo
  WriteStdout "Strange, couldn't find the GetProcessMemoryInfo function...~n"
  Return
EndIf
Local PMC:TProcessMemoryCounters = New TProcessMemoryCounters
PMC.Size = SizeOf TProcessMemoryCounters
Local N = GetProcessMemoryInfo ( GetCurrentProcess ( ) , PMC , SizeOf TProcessMemoryCounters )
If Not N
  WriteStdout "There was an error when calling the function...~n"
  Return
EndIf
N = PMC.WorkingSetSize
WriteStdout "This process takes " + N + " bytes in memory.~n"
WriteStdout "The taskmamager shows this size probably as " + N / 1024 + "K.~n"

Type TProcessMemoryCounters
  Field Size
  Field PageFaultCounter
  Field PeakWorkingSetSize
  Field WorkingSetSize
  Field QuotaPeakPagedPoolUsage
  Field QuotaPagedPoolUsage
  Field QuotaPeakNonPagedPoolUsage
  Field QuotaNonPagedPoolUsage
  Field PagefileUsage
  Field PeakPagefileUsage
EndType

Extern "Win32"
  Function GetCurrentProcess ( )
  Function LoadLibraryW ( Name:Short Ptr )
  Function GetProcAddress:Byte Ptr ( Lib , Name:Byte Ptr )
'  Function GetProcessMemoryInfo ( Process , MemCounters:Byte Ptr , Size ) 'Doesn't work, seems like BlitzMax doesn't
                                                                           'directly link to Psapi.dll (a bug?)
EndExtern
P.S.: I assumed you wanted to get this information about the process where you called the function. If not you need first to create a handle to the process, which you want to get the information about. To get this handle you need either
- the process id
- a handle to a thread which belongs to the process
- an id of such a thread
- a handle to a window created such a thread
- (not secure) the caption of such a window
But as long as you just need this information for the current process, the code above should work.