Monitoring for memory leaks

I am using three different DLLs in the application I’m building. There is the possibility of memory leaks creeping in from these DLLs. I have a window in my app where I’m monitoring status of various pieces of external hardware and I’d like to include an ongoing status indicator of how much memory my app is using so I can keep an eye on this.

I see that I can use Runtime for this – but my question is, does Runtime’s MemoryUsed property also report memory being used by libraries my app is using? That’s the thing I’m most concerned about.

No it only includes memory allocated by Xojo. If the DLL allocates memory internally Xojo won’t know about that. However, macOS Activity monitor and Windows task manager will show you total utilisation, including the DLLs. Together you will be able to look for memory leaks.

Thanks. Is there a way to get tthe DLL memory usage into my app?

The UI for my app basically has to be full screen so I don’t have room to have the task monitor up (or space for a second monitor for this). Really mostly interested in Windows, since that’s the only target for this app but I am doing a lot of the dev work on mac, so having it there too would be handy.

There are tons of ways to do it. Some examples: How To Check RAM Usage in Windows | Tom's Hardware

The Command Prompt part could be used within xojo with the shell class

Thanks. I was hoping though, for a declare or some kind of system-level call, rather than using the command line. I’m aware of those tools, but it’s more effort to have to call it and parse the results, than it would be if there are some system-level libraries that will share this info. I have no idea what those would be though, if they exist.

If you want the API route: Process Status API - Win32 apps | Microsoft Learn

I’m not a Windows-focused user, but I seem to remember you can make the tasks manager to float above all other windows (look at its menu items).

1 Like

I would suggest you file a feature request. Other tools have memory monitoring and leak tracking built right in. I suspect that Xojo knows this as they have to use them to make their own development tool.

Screenshot 2023-11-21 at 3.46.29 PM

Doesn’t the Xojo runtime memory used function just ask the OS for what number of bytes it allocated?

Because I think it’s not just a counter of allocations for Xojo.

I got a test app to show it:

// DECLSPEC_ALLOCATOR HGLOBAL GlobalAlloc([in] UINT   uFlags,[in] SIZE_T dwBytes);

declare function GlobalAlloc lib "Kernel32" (flags as Uint32, bytes as UInteger) as Ptr

const GMEM_FIXED = 0

for i as integer = 1 to 100
  
  dim p as ptr = GlobalAlloc(GMEM_FIXED, 1024 * 1024)
  
  if p = nil then Break
  
  // allocating is not enough! You need to use it!
  for j as integer = 0 to 1024*1024-1 step 1024
    p.int32(j) = j
  next
  
  System.DebugLog "memory used: "+str(runtime.MemoryUsed)
  
next

Break

Memory usage goes up one MB per iteration.