MemoryAnalysis Extension
The Memory Analysis extension is designed to help you understand how memory is used in Appway.
Usage Instructions
To analyze an environment, follow these steps:
- 
                                                        
Install the MemoryAnalysis extension in your environment.
 - 
                                                        
Configure the extension as needed. To start with, you can use the default settings. Later on you can consider enabling:
- Tracking session sizes See Session Analysis Attribute Listener for more details.
 - Tracking memory allocations in HTTP threads See Memory Allocation Analysis Filter for more details.
 
 - 
                                                        
If you configured the extension to track session sizes, configure the extension to run at startup (System Configuration > Extensions > MemoryAnalysis: Run at startup) and restart your environment.
 - 
                                                        
Check the output from each node under
data-home/logs/memory-analysis/to analyze memory usage for the time period since the extension was installed. 
You can also update the JVM arguments to log the Garbage Collection logs in a separate file. Configure the following arguments:
-XX:+PrintGC-XX:+PrintGCDetails-XX:+PrintGCDateStamps-Xloggc:/[DataHome]/logs/memory-analysis/gc.logReplace[DataHome]with the correct path.
Implementation
The extension registers the Memory Analysis job which prints aggregated statistics about used memory to files in the Appway data home on each node (under logs/memory-analysis/).
The Memory Analysis job collects many statistics by default, but two parts need to be enabled explicitly:
- If enabled, the extension installs a Servlet Filter which tracks how much memory is being allocated in every HTTP request. If the amount of allocated memory exceeds a configured threshold, a warning is logged and detailed information is written to another file in the Appway data home on each node (under 
logs/memory-analysis/). For HTTP requests where Processes are involved, the Processes that are being executed are logged. See Memory Allocation Analysis Filter for more details. - If enabled, the extension registers an HTTP session attribute listener that enqueues sessions to be analyzed in a separate thread. It computes the memory occupied by attributes stored in the session. See Session Analysis Attribute Listener for more details.
 
The Memory Allocation Widget
The Memory Allocation Widget is a Screen Component that can be used to track the memory allocation in HTTP requests while developing Appway Solutions. The widget can be dragged, collapsed and positioned anywhere in a Screen.
The following properties of the Memory Allocation Widget can be configured in Appway Studio (Properties tab on the Screen where the Screen Component is located):
- Refresh interval (ms) — Time interval at which the widget content is updated. Default value is 3000 (3s).
 - Warning threshold (MB) — Memory allocation threshold under which HTTP requests are shown as green. Default value is is 200.
 - Error threshold (MB) — Memory allocation threshold over which HTTP requests are shown as red. Default value is is 500.
 
The behavior of the Memory Allocation Widget can also be configured by changing the following properties in the MemoryAnalysis extension configuration (System Configuration > Extensions):
com.nm.memoryanalysis.memory.allocation.widget.max.user.requests— Number of HTTP requests that are remembered and displayed in the widget. Default value is 10.com.nm.memoryanalysis.memory.allocation.widget.min.allocation.bytes— Threshold under which HTTP requests that allocate small amounts of memory are not tracked. Default value is 5242880 (5MB).
The Memory Analysis Job
This job runs at regular intervals (by default every hour at minute 39 and 30 seconds). The execution schedule can be changed by configuring the following property: com.nm.memoryanalysis.job.cron.expression
The job retrieves the cached Memory Holder statistics and writes them to a file in the Appway data home on each node (logs/memory-analysis/stats.log).
The list of additional Memory Holders which are deployed by the extension currently includes:
- Memory Allocation Statistics
 - HTTP Session Attributes
 
The Memory Allocation Analysis Filter
This Servlet Filter collects the memory allocated by this thread before and after processing an HTTP request. If the difference is larger than the configured threshold, a warning is logged and detailed information is written to a file in the Appway data home on each node (logs/memory-analysis/allocation.log).
In addition, another Servlet Filter is registered, ProcessIdsCacheFilter. This filter caches the Processes that are currently being executed. This means that for each request that allocates a large amount of memory, you can  easily identify the underlying Processes.
These memory allocation warnings can then be correlated with large Garbage Collection pauses.
Enable and configure the MemoryAllocationAnalysis Servlet Filter using the following configuration properties:
com.nm.memoryanalysis.track.allocated.bytesEnables the analysis of the allocated memory iftrue. Default:falsecom.nm.memoryanalysis.allocated.bytes.log.thresholdSpecifies the threshold for allocated memory size after which the Servlet Filter logs request details, in bytes. Default: 524288000 (500MB)
The Session Analysis Attribute Listener
The sizes of HTTP sessions are computed every time the attributes of the sessions are changed. SessionAnalysisAttributeListener is a HTTP session attribute listener that intercepts all session attribute changes and adds the session to a FIFO queue to be processed.
Further characteristics of the listener are:
- The stats for a session are processed with a delay. This ensures that in cases where many attributes are changed in the same request, computation starts only after all attributes have been changed. There is only one thread configured to compute statistics about HTTP sessions.
 - If the session was changed while the computation was in progress, the stats are not stored, otherwise the computed statistics are stored in a map. This map is cleaned every time a session is destroyed.
 - When the Memory Analysis job runs, it also computes the overall sizes of the current sessions.
 SessionAnalysisAttributeListenercan only be registered during startup. This means that if the extension is started while Appway is running, the session statistics are not available until after the next restart. The listener cannot be unregistered when the extension is stopped, but all processing is stopped and the stored data is cleared if the extension is not running.
Enable and configure the SessionAnalysisAttributeListener using the following configuration properties:
com.nm.memoryanalysis.track.session.sizesEnables tracking of session sizes iftrue. Default:falsecom.nm.memoryanalysis.session.size.log.thresholdSpecifies the threshold for the session size after which the servlet filter logs session details, in bytes. Default: 52428800 (50MB)com.nm.memoryanalysis.session.size.exclude.JSONRPCBridgeIncludes the analysis of the content of the JSONRPCBridge session attribute iffalse. Default:trueThe JSONRPCBridge session attribute is not analyzed by default, because it can reference Appway internal services which you want to exclude when computing the session size.
Note: Computing session sizes is quite CPU expensive and might require up to one core on each node of your installation. Since a single thread is used for this computation, no more than one core is used per node.