Page 1 of 4
[Solved] My app slows down other apps?
Posted: Thu Jul 11, 2024 10:55 am
by BarryG
SOLVED! Turns out I had a
lot of regular loaded icons that were never being destroyed after use. Now my GDI count doesn't rise.
Original post:
Hi all. I've noticed for some time now that my app, after it's been running for a few hours, starts to affect the speed and GUI of other apps.
In the gif below, I open a third-party Calc app twice and you can see it opens and draws itself SLOWLY. I then quit my PureBasic app and then open Calc twice more, and it's drawn instantly.
I have no idea why my PureBasic app can affect the speed and GUI of others?
I obviously can't post the code here, but it uses threads (with safety on), and the CPU use of my app is never more than 2% in Task Manager. It's not a graphics card issue as the drivers are up to date, and the problem goes away when my app quits.
Anyone ever experienced this sort of thing before? Do you think it may be an API command causing it? Because my app does check the foreground window for info, so maybe I should start looking there? Maybe my code is blocking messages to the foreground window? Is that even possible? Just looking for concept answers at the moment for discussion.
Using PureBasic 6.10 (but also happens with 6.11) on Windows 10 Pro (desktop) and Windows 11 Pro (laptop). Same result on both PCs.

Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 11:43 am
by NicTheQuick
Well, we can only guess without seeing your code.
Can you pin it down to certain things you do in your program?
For example:
- Are the 2% CPU usage to be expected from your app? An idle application should only consume 0% CPU.
- Do you create a lot of window events accidentally? This could probably happen due to wrong event handling where new events were created while a certain event is being processed. And it would also explain why the event handling and drawing of the buttons in the calculator is delayed too. The Windows event system is handled globally for all windows across all processes in the backend if I am right.
- Your application might create a lot of I/O load due to file handling or stuff like that. You won't notice this in the CPU consumption but only in the general system load.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 12:47 pm
by BarryG
The app's full data size to compile is 18 MB (resources, images, sounds, etc), so sharing is not possible.
The 2% CPU usage is normal; it does some background stuff every few seconds.
The lag isn't caused by disk access because it doesn't do that continually.
I think what you said about the Windows event system being global might be a clue! Maybe I should be using PostMessage in some areas instead of SendMessage, because SendMessage waits for the other window to respond. Hmm. Thanks, I'll check that out!
The main reason for asking is because it's all good when my app starts: the lag with other apps isn't there. It's only after my app has been running for a few hours that other apps start to work slowly, which makes it a pain to debug because you have to wait hours before trying to guess why.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 1:06 pm
by #NULL
Did you check memory consumption of your program? If a memory leak uses up memory and swap becomes more and more necessary, it can lead to sluggish system performance(?)
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 1:38 pm
by BarryG
I thought it might be that, but it's not a memory leak. It uses around 12 MB of RAM while running and it doesn't rise from that after many hours.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 3:30 pm
by NicTheQuick
What about your threads? Do you create a lot of threads over time that do not exit properly?
Since the memory consumption of your process does not increase over time I don't think that could be the issue, but you never know.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 4:08 pm
by skywalk
Maybe you can simulate the sluggishness by opening other heavy programs and/or changing your app's update frequency?
Install Teams and create multiple dummy meetings. That code is a pig.

Do you see sluggishness without your app?
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 6:02 pm
by idle
I would say it's a gdi problem but then they fixed gdi a Long time ago. Are you leaking handles.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 6:24 pm
by AZJIO
Do you use hooks to intercept events from other windows? If you capture an event, it can be absorbed or passed on to the queue for the application to receive it. Interception or suspension by any action may affect other windows.
Re: My app slows down other apps?
Posted: Thu Jul 11, 2024 7:01 pm
by spikey
Get Sysinternals from
https://learn.microsoft.com/en-gb/sysinternals/. There are a number of tools which can be useful in diagnosing more complex issues. It's been an indispensable part of my toolkit for
many years.
I'd start with "Process Explorer", select Show Lower Pane from the View menu so you can see Handles, DLLs and Thread usages.
Re: My app slows down other apps?
Posted: Fri Jul 12, 2024 10:32 am
by Fred
Yes Handle might be the issue, you can watch this in system monitor (you can add a column with handle count)
Re: My app slows down other apps?
Posted: Sun Jul 14, 2024 6:01 am
by BarryG
skywalk wrote: Thu Jul 11, 2024 4:08 pmDo you see sluggishness without your app?
No, only after my app has been running for a few hours. No slowdown when my app first starts.
idle wrote: Thu Jul 11, 2024 6:02 pmAre you leaking handles
Ah, this might be it! I did some testing today and I was loading "kernel32.dll" as a global (since it's used a lot in various procedures):
Code: Select all
Global kernel32=OpenLibrary(#PB_Any,"kernel32.dll")
Then, some parts of my code (in procedures) were like this:
Code: Select all
snap=CallFunction(kernel32,"CreateToolhelp32Snapshot",#TH32CS_SNAPPROCESS,0)
If snap
Proc32.PROCESSENTRY32\dwSize=SizeOf(PROCESSENTRY32)
While CallFunction(kernel32,"Process32NextW",snap,@Proc32)
; Do something here.
Wend
CloseHandle_(snap) ; <- Maybe this shouldn't be done?
EndIf
And I'm thinking now that maybe I SHOULDN'T be doing "CloseHandle_(snap)" in there? Will that close the loaded "kernel32.dll" library? Because I don't want to close that DLL until my app quits. I'll run my app for a few hours as a test to see.
Re: My app slows down other apps?
Posted: Sun Jul 14, 2024 10:02 am
by idle
What I meant was to see if the handle count of your app keeps growing. Take a look at sysinternals tool process explorer mentioned above and watch the handle count, If it constantly grows there's a problem.
I don't know if you should be calling Free handle or not. I'm on mobile atm.
Re: My app slows down other apps?
Posted: Sun Jul 14, 2024 10:18 am
by BarryG
idle wrote: Sun Jul 14, 2024 10:02 amTake a look at sysinternals tool process explorer mentioned above and watch the handle count, If it constantly grows there's a problem.
Ah, I see what you mean. Okay, so my app has 39635 handles in the count, which keeps rising every time I quit and restart Process Explorer. So yes, they're rising. And this number is way higher than any other process on my PC, which typically have around 2500 handles or less.
I just quit and restarted Process Explorer again and now my app has 41811 handles. Far out. What do I do to stop this? Thanks for any tips!
I read this, but it's over my head ->
https://stackoverflow.com/questions/600 ... es-to-grow
For reference, when my app starts, it has around only 500 handles.

Re: My app slows down other apps?
Posted: Sun Jul 14, 2024 10:30 am
by infratec
Why not:
Code: Select all
EnableExplicit
Import "kernel32.lib"
CreateToolhelp32Snapshot.i(dwFlags.l, th32ProcessID.l)
Process32NextW.i(hSnapshot.i, *lppe)
EndImport
Define snap.i, Proc32.PROCESSENTRY32
snap = CreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0)
If snap <> #INVALID_HANDLE_VALUE
Proc32\dwSize = SizeOf(PROCESSENTRY32)
While Process32NextW(snap, @Proc32)
Debug RSet(Str(Proc32\th32ProcessID), 5) + ": " + PeekS(@Proc32\szExeFile[0])
Wend
CloseHandle_(snap)
EndIf
Then you don't have problems.
And yes, if you create a handle, you have to close it.
And you have to check for <> #INVALID_HANDLE_VALUE
Because this value is also 'true', it is not 0