[Solved] My app slows down other apps?

Everything else that doesn't fall into one of the other PB categories.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

[Solved] My app slows down other apps?

Post 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. :D

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.

Image
Last edited by BarryG on Wed Aug 14, 2024 1:34 am, edited 9 times in total.
User avatar
NicTheQuick
Addict
Addict
Posts: 1518
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: My app slows down other apps?

Post 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.
Last edited by NicTheQuick on Thu Jul 11, 2024 1:37 pm, edited 1 time in total.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: My app slows down other apps?

Post 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(?)
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1518
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: My app slows down other apps?

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
skywalk
Addict
Addict
Posts: 4216
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: My app slows down other apps?

Post 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. :lol:
Do you see sluggishness without your app?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: My app slows down other apps?

Post by idle »

I would say it's a gdi problem but then they fixed gdi a Long time ago. Are you leaking handles.
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: My app slows down other apps?

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 767
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: My app slows down other apps?

Post 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.
Fred
Administrator
Administrator
Posts: 18204
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: My app slows down other apps?

Post by Fred »

Yes Handle might be the issue, you can watch this in system monitor (you can add a column with handle count)
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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.
User avatar
idle
Always Here
Always Here
Posts: 5891
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: My app slows down other apps?

Post 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.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: My app slows down other apps?

Post 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. :(
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: My app slows down other apps?

Post 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
Post Reply