Back

Topic

[KB1115]How to restart Windows automatically in case of memory leak?

Tags: Audit, Diagnostic, Memory leak

4 years ago
By LM
Options
Print
Applies to:

PcVue 11 onwards


Summary:

This article explains how to restart Windows if a memory leak is present in a PcVue project. The origin of the memory leak may be difficult to find in some cases. While waiting for the leak to be found and fixed, it can be useful to avoid the application crash by rebooting the system at the right time.


Details:

A PcVue project can consume up to 4GB of RAM on a 64-bit operating system. We know from experience that if the virtual memory available for PcVue drops below 100 MB, the application could crash. The variable @SYSTEM.LocalHost.Resource.FreeVirtualMemory monitors this amount of available memory.

There are several ways to restart the application at the right time:

  1. We can add a low threshold on this variable to trigger a restart of the project (see How to restart a PcVue project – svrestart.exe) when the value reaches 100000.
    We can also trigger a full system restart if PcVue restarts automatically when Windows starts
    SYSTEM(“SYSTEM”, “shutdown /r /t 120”); ‘——– Restart Windows after 120 seconds
    SYSTEM(“EXIT”); ‘——– Stop PcVue
  2. Another way would be to check system variable value periodically in order to better control restarting time. For example every day at 5am or once a week, thanks to a scheduled action.
    If you have a redundant architecture, the best is to create 2 scheduled actions to be sure that the 2 stations never stop at the same time:

Scheduled actions

Here is an example of a Scada Basic function launched by 2 scheduled actions at different times:

‘* Stop PcVue if memory becomes too low
‘***********************************************
Sub RebootWindows()
Dim Station as single;
Station = SVAL(GetArg(“ARG1”));
If (Station == @SYSTEM.STATION_NUMBER) Then
If (@SYSTEM.LocalHost.Resource.FreeVirtualMemory < 200000) Then ‘——– 200000 recommanded value
TRACE(“LOG”, “We force the passive switchover of the server because it does not have enough memory”);
SYSTEM(“SYSTEM”, “shutdown /r /t 120”); ‘——– Restart Windows after 120 seconds
SYSTEM(“EXIT”); ‘——– Stop PcVue
ELSE
TRACE(“LOG”, “Sufficient available memory”);
End If
Else
Print(“I am not the station concerned”);
End If
end sub

The station number is provided by the argument passed via the scheduled action. In this case we check that the available virtual memory is higher than 200000 to have a safety margin. This threshold must be adapted according to the memory leak.

  1. On some server Operating Systems, SYSTEM instruction SYSTEM mode will not work. It is then necessary to use a Windows scheduled task that you can define as being launched as an administrator.
    Then you just have to launch this scheduled task using a batch file.
    This batch can be executed from a Scada Basic program using APPLICATION instruction and LOAD mode as shown below:

‘* Stop PcVue if memory becomes too low
‘***********************************************
Sub RebootWindows()
Dim Station as single;
Station = SVAL(GetArg(“ARG1”));
If (Station == @SYSTEM.STATION_NUMBER) Then
If (@SYSTEM.LocalHost.Resource.FreeVirtualMemory < 200000) Then ‘——– 200000 recommanded value
TRACE(“LOG”, “We force the passive switchover of the server because it does not have enough memory”);
APPLICATION(“LOAD”, “reboot.bat”); ‘——– Execute a batch located in TP directory of the project in order to restart Windows after 120 seconds
SYSTEM(“EXIT”); ‘——– Stop PcVue
ELSE
TRACE(“LOG”, “Sufficient available memory”);
End If
Else
Print(“I am not the station concerned”);
End If
end sub


Created on: 15 Jun 2021 Last update: 09 Apr 2025