KB0044 - High memory usage in a long running, complex App

KB0044 - High memory usage in a long running, complex App

Problem

Memory usage can grow continuously in a complex app that is either long running and\or very busy. 

Root Cause

Apps maintain user state as the users interact with the app. This state includes variables that are set and event handlers that are defined within the app. Even if the user closes the tab, the state is maintained in order to support browsers that automatically reconnect after pausing the page, such as mobile web browsers. After 25 minutes (the default session timeout), the session and tab state will be released if the user has not accessed the page again. If an app is busy, with many users connecting and disconnecting from the app, it can continue to consume memory at an increasing rate. 

While it is possible to reduce the session timeout value, it may not be ideal. 

Workaround

As of PowerShell Universal 4.0.x. page state does not offer a specific timeout value and is tied to the session timeout value. Using the following sample, you can time out pages that haven't been accessed for 1 minute. The sample will also list the currently saved page states for the current session. Refreshing the page will produce a new page state. After 1 minute, the page state will begin to disappear from the tab. This is a sign that the event handlers and variables are being released and memory is being freed. 
  1. $EndpointSchedule = New-UDEndpointSchedule -Every 1 -Minute
  2. $Endpoint = New-UDEndpoint -Schedule $EndpointSchedule -Endpoint {
  3.     $EndpointService.SessionManager.Sessions.Values | ForEach-Object {
  4.         $_.Pages.Values | ForEach-Object {
  5.             $OneMinuteAgo = [DateTime]::UtcNow.AddMinutes(-1)
  6.             if ($_.LastTouched -lt $OneMinuteAgo) {
  7.                 $_.LastTouched = [DateTime]::MinValue
  8.             }
  9.         }
  10.     }
  11.     $TimeSpan = New-TimeSpan -Minutes 25
  12.     $EndpointService.SessionManager.ClearTimedOutSessions($TimeSpan);
  13. }
  14. New-UDApp -Title 'PowerShell Universal' -Content {
  15.     New-UDDynamic -Content {
  16.         New-UDDynamic -Content {
  17.             New-UDButton -OnClick {
  18.             }
  19.         }
  20.     }

  21.     New-UDDynamic -Content {
  22.         $Pages = @()
  23.         $EndpointService.SessionManager.Sessions.Values | ForEach-Object {
  24.             $_.Pages.Values | ForEach-Object {
  25.                 $Pages += $_
  26.             }
  27.         }
  28.         New-UDTable -Data $Pages -Columns @(
  29.             New-UDTableColumn -Property 'Id'
  30.             New-UDTableColumn -Property 'LastTouched'
  31.         )
  32.     } -AutoRefresh -AutoRefreshInterval 3
  33. }

    • Related Articles

    • KB0050 - High memory usage for apps\dashboards using UDElement event handlers

      Affected Versions - PowerShell Universal 4.1.1 and earlier - PowerShell Universal 3.9.17 and earlier Problem PowerShell Universal apps (v4) and dashboards (v3) will consume gradually more memory when being accessed by users when the dashboard is ...
    • KB0012 - High Availability

      Update Please see the documentation at https://docs.powershelluniversal.com/config/hosting/high-availability for in-depth details on this topic. Below is an overview statement from Adam Driscoll on this topic as well. Adam's statement Our high ...
    • KB0046 - Collecting a Memory Dump

      Purpose The purpose of this document is to provide steps for collecting a memory dump. Occasionally, Ironman Software support may request a memory dump of one or more of the processes involved with the functioning of our software. These memory dumps ...
    • KB0056 - App Pages Are Missing After Service Restart

      Affected Versions - PowerShell Universal 4.0.0 through 4.2.5 Problem When restarting the PowerShell Universal service, pages that were defined in Apps are no longer displayed in the admin console. The apps are still functional, but they cannot be ...
    • KB0063 - API, App or Job Not Starting

      Problem External processes do not start after upgrades or with the same configuration and script on different machines. Common errors in the log will include: [ERR] Dashboard process running but not response after 10 seconds. Terminating process. ...