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

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 634
Joined: Fri Dec 04, 2015 9:26 pm

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

Post 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?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

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

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

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

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5387
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 634
Joined: Fri Dec 04, 2015 9:26 pm

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

Post 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?
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

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

Post by Keya »

for your Windows process memory usage - viewtopic.php?f=13&t=76909
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

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

Post 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
地球上の平和
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

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

Post 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.
Proud supporter of PB! * Musician * C64/6502 Freak
Post Reply