Page 1 of 1

Have a way to limit RAM USAGE and CPU USA of a application ?

Posted: Sat Mar 27, 2021 6:00 am
by skinkairewalker
Hello guys, is there any way to limit the RAM usage and CPU usage of an application?

External program or even some algorithm that as soon as the memory usage reaches the limit the application no longer uses until a certain memory is released...

to limit the CPU I imagine that if I add a "delay (1)" in any loop, it will reduce the CPU usage a lot, however, I don't know if it is effective ...

can someone help me?

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Sat Mar 27, 2021 7:08 am
by TI-994A
skinkairewalker wrote:Hello guys, is there any way to limit the RAM usage and CPU usage of an application?
In event loops where the WindowEvent() function is used instead of the WaitWindowEvent() function, the Delay() function should be included to prevent CPU hogging. Besides this, blocking-functions could also hog the CPU, and should be mitigated with timers, or with threads and semaphores.

As for memory usage, PureBasic maintains reference counting and does not release allocated resources unless explicitly done so. This is achieved by the various FreeXXX() functions, by nulling out variables, and by explicitly shrinking the string buffers through reduced allocations. Furthermore, procedural recursions could also cause memory leaks and stack overflows, and should thus be used with utmost care.

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Sat Mar 27, 2021 12:56 pm
by Bitblazer
@skinkairewalker
The use of delay() in some loops is essential for a fluid application / OS feeling for some operating systems.

Basically it helps the scheduler to distribute CPU resources.
TI-994A wrote:Besides this, blocking-functions could also hog the CPU, and should be mitigated with timers, or with threads and semaphores.
Thats why i consider missing (tightly integrated) asynchronous calling methods to be one of the biggest problems for PB.

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Sat Mar 27, 2021 7:23 pm
by mk-soft
Bitblazer wrote:Thats why i consider missing (tightly integrated) asynchronous calling methods to be one of the biggest problems for PB.
Which missing asynchronous calling methods?

There are threads for this and the finished message can be sent to the GUI via PostEvent.

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Mon Mar 29, 2021 2:08 pm
by skinkairewalker
thanks all ( TI-994A - Bitblazer - mk-soft )

but can I at least "measure" the amount of ram being used? to avoid that my users do not use more than the limit that I pre-establish?

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Mon Mar 29, 2021 6:59 pm
by Keya
for your Windows process memory usage - viewtopic.php?f=13&t=76909

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Mon Mar 29, 2021 10:28 pm
by Saki
In the Digi-clock code you will find a function which calculates the delay automatically.

You can see here in the gif how the delay is automatically adjusted.

When another function requests resources, the delay decreases again and levels itself again.

To do :
Look at the code.
You have to deal with this mechanism.
You have to create two parts, a sensor and a procedure to evaluate the sensor.
You can also copy out the function. :wink:

viewtopic.php?f=12&t=76880

Image

Re: Have a way to limit RAM USAGE and CPU USA of a applicati

Posted: Tue Mar 30, 2021 5:41 pm
by oreopa
skinkairewalker wrote:but can I at least "measure" the amount of ram being used? to avoid that my users do not use more than the limit that I pre-establish?
It totally depends on the application I suppose, but I surely imagine you can keep track of roughly how much memory you are using by keeping a tally of the size of the objects you create/load/whatever in your program (at least an estimate). This seemed sufficient for my needs.

I would also like a solution for CPU limiting (by CPU percentage or so), however. I didn't look into it closely, but I think on most OS you can set the process priority which may get us somewhere close to it... but then again with threads, it's probably a bit harder.