Question

I am doing a delphi application that will run on my pc 24/7 in the background and will check if it has to do some actions or not, wait 30 minutes and check again, and so on.

How can I make sure the application will not overload cpu or memory because of being running all the time.

Was it helpful?

Solution

Most programming languages have a "sleep" function that you can call to make the program stop doing stuff.

You are in control of the memory usage; you can deallocate your memory before going to sleep and reallocate it when coming awake, but...

It might be better to just set up some recurring job. I don't know how to do this, but I suspect there's a way with Windows Scripting Host to just launch your application on whatever schedule you want.

OTHER TIPS

Create a timer to run every 30 minutes, and call your checks/actions from there. Then your application can just sit idle when there is nothing to do.

Alternatively you could create a Scheduled Task that just runs periodically to do this.

The answers about timers are good solutions, and I add this: Make sure that the timer event, or subsequent procedure called, checks for busy. i.e. if you wake up, make sure that the last batch is done before starting a new batch. This is easy to miss when things are flowing well, and then you have a situation where things are backed up and the whole system logjams at 8 in the morning because something bad happened at midnight and now there are now 16 calls stacked up (or threads, processes, etc..).

So write your timer event like this:

OnMyTimer...
begin
  MyTimer.Enabled := false;
  try
    DoSomethingForALongTime;  // Usually runs in 45 seconds, but sometimes takes 45 minutes!
  finally
    MyTimer.Enabled := true;  // and (SomeAbortRequest = False) and (SomeHorribleErrorCount = 0);  
  end;
end;

The answers about timers are pretty much exactly what you're looking for. As for your question about not overloading the CPU or memory, take a look at your program in the Task Manager. When it's not doing anything, it should sit at a "steady state" of memory, not allocating any more, and using 1% or less of CPU time.

It's safe to let most programs idle for long periods. The VCL knows how to handle the idling without hogging CPU resources for you, and you just need to use a timer to make sure it wakes up and activates its event at the right time.

If you want to enforce an absolute state of inactivity, I guess you could use the "sleep" function. Though I'm not sure how it would behave on a reboot. (I guess Windows would report the application as being unresponsive.)

If the application has no main form and just sitting in the tray (Or being totally invisible), it won't do much. The main message loop will handle all message it receive from the OS, but it shouldn't receive many. And the few message it will receive, it should process them (Shutdown messages, System parameters change notification, etc)

So, I think you could just set up a timer and forget about setting code to force your program to stay idle.

If you really want to limit that process activity to a maximum, you could set the thread priority when you enter/leave the timer's event. So you would set the priority to "normal" when you enter the event, and set it to "Low" when getting out of it.

You didn't tell, but if your application uses more than one thread, this could add to the amount of CPU the OS spends on your process (read up on time slices and thread-switches for example).

The OS may also swap out some of your memory pages, thus using less memory and/or reducing memory-accesses in the "wait-time" helps too.

So, if you use only one thread and have no additional message-loops either, just calling Sleep() could be a good way, as that will signal the OS that you don't need a time slice at all for a long while to come.

Avoid YieldThread()/SwitchToThread() and your own time-keeping (using Now() for example) as that would mean lots of thread-switching is taking place, to do .... nothing!

Another method could be to use WaitForMultipleObjects with a large timeout, so your application can still respond to messages.

I would suggest creating a service application (set startup type as automatic) and use CreateTimerQueueTimer as your timer. Memory leaks can be mitigated by reserving memory requirements/pooling classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top