Child pages
  • Memory Management

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Memory Management for Java applications is an extensive topic with loads of tuning options. However, the Java Virtual Machine by default takes care of many optimizations on it's own.

Unless explicitly specified, the JVM determines the amount of memory available on the machine and depending on it's findings sets the initial and maximum sizes for different block of memory (called spaces) a Java application uses. As Java works works with explicit maximum sizes for the different spaces, if the application requires more memory than the maximum set, OutOfMemory exceptions occur, which Servoy is built using Java technology and as such the memory management for Servoy is basically Java Memory management. This chapter describes the basics of Java Memory management, which will be sufficient all but the most extensive and complex Servoy environment. 

Java Memory management introduction

The basics of Java Memory management is straight forward:

  • A Java process uses several memory spaces (called the Perm Space or Heap Space for example), each of which use a dedicated block of memory
  • For each space the Java process determines the initial and maximum amount of memory it is allowed to use, of not explicitly specified through configuration. The defaults values differ per JVM implementation and can depend on the hardware on which the JVM is started.
  • The initial and maximum's for each space can also be explicitly specified through configuration
  • The initial memory sizes for all spaces are allocated at startup of the Java process
  • The Java process will allocate more memory only when required, up the specified maximum per space
  • Allocated memory that is not used anymore will periodically be released in a process called Garbage collection. In Java this is a fully automated process
  • If the Java process requires more memory than the maximum in any of the spaces, the Java process will throw a relevant exception, for example an OutOfMemory or Stack Overflow exception. These exceptions should be prevented as they both degrade performance, but more importantly can cause unexpected behavior.

Balancing act

Java's memory management works with automatic Garbage collection: the process of removing objects from memory if they are no longer used, to ensure the Java process doesn't quickly run out of memory. This is a fully automatic process, but the performance of the Java process might be compromised when using the wrong configuration.  Managing the memory usage of a Java process is a balancing act between stability and performance: Just allocating tremendous amounts of memory to the Java process so it never runs out of memory (ensuring stability) can have a negative impact on performance. 

Java Memory spaces

The two most important spaces of memory that a Java process uses are the Java Heap Space and the Java Perm Space. 

...

Java Heap Space

...

Java Perm Space

...

For more information on Java Memory management, see http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf

Server Memory management

Dimensioning the Server

The memory consumption of a Servoy Application Server consists of the following parts:

  • The Servoy Application Server itself
  • The memory usage related to active/idle database connections
  • The memory usage related to running Servoy Clients

The memory usage of the Servoy Application Server itself is limited to a couple of Mb.

For the database connections assume 2Mb per connection, so sum the Maximum Active connections settings on each configured and enabled Database Server and multiply that by 2Mb to determine the maximum memory consumption by the database connections

The memory usage for the running Servoy Clients is more difficult to obtain, as it depends on the number of running Clients, the type of the running Servoy Clients, the way the Servoy Solutions that are running are built and the way the users use the Solutions. These variables together make it difficult to provide hard numbers on how to configure the memory settings beforehand. Getting it right means monitoring and tuning.

As a starting point the following rule of thumb can be used to determine the initial settings: 

  • Smart Client using the Servoy HTTP Tunnel:  assume 900kb for each Smart Client
  • Smart Client using another connection mode that the HTTP tunnel: assume 600kb for each Smart Client
  • Web Client, Headless Client & Batch processors: the required memory for each client depends on the size and design of the solution, but should be in the order of magnitude of a couple of Mb per Client

Smart Client Memory usage vs. other Servoy Client

The memory consumption of Smart Client on the Server is an order of magnitude lower that for the other Servoy Clients, as the Servoy Smart Client runs on the client machine, not on the Servoy Application Server. See #Smart Client Memory management for more information on configuring the Memory settings for Smart Clients.

Web Client, Headless Client & Batch processors memory usage

The memory usage of Web Clients, Headless Clients and Batch Processors depends highly on the design of the solution. While Servoy optimizes many things to keep the memory footprint as low as possible, it is logical that a simple solution showing just one form at the time has a different memory footprint than a solution that has a very complex UI, showing 20 Forms and/or different sets of data at the same time.

The memory footprint is not linear with the number of Forms a Solution contains, as Forms are instantiated on a need-to basis.

By default the maximum heap size is set to 256MB. This setting should be changed when:

  • The actual used memory is >= 70% of the specified maximum. The actually used memory can be found on the main page of the Admin page, under System Information, like: Heap memory: allocated=549184K, used=371473K, max=699072K
  • When the expected client load will exceed the maximum. Using the following rule of Thumb:
    • Smart Client: assume a memory usage of about 600kb for each Smart Client. NOTE: When the Servoy HTTP Tunnel is used, assume 900kb per Client.
    • All other type of Clients (Web, Headless, Batch): the required memory for each client depends on the size and design of the solution, but should be in the order of magnitude of a couple of Mb per Client.
  • When there is plenty of free real memory available on the OS level. Java processes in general perform better when not having memory constraints. For example, when 2Gb free real memory is left, add the 2Gb to the maximum heap size. The Java process will only take what it needs.

There is a big difference in  

...

HTTP Tunnel usage

...

32bit Java Mem assignment limitations

...

Configuring the memory settings

...

Manual start: servoy_server.bat/.sh

...

{serverInstall}\application_server\servoy_server.bat/.sh

...

Auto start: wrapper.conf

...

{serverInstall}\application_server\service\wrapper.conf

...

Starting in Servoy Cluster: start_servoy_clustered.bat/.sh

...

{serverInstall}\application_server\terracotta\start_servoy_clustered.bat/.sh

OutOfMemory exceptions

Smart Client Memory management

Initial Client Heap

Max. Client Heap

Virtual Machine Arguments

TODO

The maximum heap size setting is located in ../application_server/servoy_server.sh/.bat, through the "Xmx" setting.

When the Application Server is started as a Service the setting can be found in ../application_server/service/wrapper.conf, by the name of "wrapper.java.maxmemory".

Java Virtual Machine (JVM) Tuning

Java version
Servoy supports both Java 5 and Java 6 (for the Application Server, the Smart Client and Servoy Developer). As Java 6 has numerous performance improvements, it is preferred over Java 5.

If the hardware in which Servoy is running is 64bit, make sure to also run a 64 bit Java Virtual Machine. When in doubt which version is used, check the Servoy Admin page, under "Servoy Server Home" > "Servoy Server Status" > "JVM Information":

  • 64 bit: java.vm.name=Java HotSpot(TM) 64-Bit Server VM
  • 32 bit: java.vm.name=Java HotSpot(TM) Client VM
Note

An 32 bit JVM will allow a max memory of 2 Gb in total, In order to run with larger heap sizes (over 2 Gb) you have to use 64 bit JVM on a 64 bit OS

Java Virtual Machine Server mode
Java has 2 distinct operating modes: client mode and server mode. When running in server mode, the startup of the Java process will take longer and initial execution of code will be slower, but after a while the application will perform faster. The server mode is best suited for the Servoy Application Server.

Whether or not a Java Virtual Machine supports client and/or server mode depends on the hardware, see http://download.oracle.com/javase/6/docs/technotes/guides/vm/server-class.html.

If the JVM supports servermode, it will automatically detect if the machine it is running on is a "capable" machine and automatically run in server or client mode depending. The definition of "capable" may differ from Java version to Java version. For Java 5/6 this means a 2CPU, 2Gb memory machine.

Checking whether or not a Servoy Application Server runs in client or server mode can be seen on the Servoy Admin page, under "Servoy Server Home" > "Servoy Server Status" > "JVM Information":

  • Server mode: java.vm.name=Java HotSpot(TM) 64-Bit Server VM
  • Client mode: java.vm.name=Java HotSpot(TM) Client VM

It is possible to enforce the Servoy Application Server to run in server mode (assuming the JVM is capable to operate in server mode), an extra startup argument can be added to the startup of the Servoy Application Server.

In servoy_server.sh/bat:

Code Block

java -server -Djava.awt.headless=true .....

When using the Service wrapper:

Code Block
# Java Additional Parameters
wrapper.java.additional.1=-Djava.awt.headless=true
wrapper.java.additional.2=-Duser.dir="C:\Servoy\application_server"
wrapper.java.additional.3=-XX:MaxPermSize=128m
wrapper.java.additional.4=-server
Note
JVM servermode on Windows 32-bit
JVM servermode on Windows 32-bit

On Windows 32 bit environments, the Java Runtime Environment does not include support for server mode. In order to take advantage of server mode optimizations on 32 bit Windows systems, it is required to install Java Development Kit (JDK)

Memory

Processes in Java, like the Servoy Application Server, need to be told explicitly the maximum of memory they are allowed to use. This setting, the maximum heap size, is important to adjust to the load the Servoy Application Server is to handle.

The maximum heap size setting is located in ..{servoy_install}/application_server/servoy_server.sh/.bat, through the "Xmx" setting.

When the Application Server is started as a Service the setting can be found in ..{servoy_install}/application_server/service/wrapper.conf, by the name of "wrapper.java.maxmemory".

...

  • The actual used memory is >= 70% of the specified maximum. The actually used memory can be found on the main page of the Admin page, under System Information, like: Heap memory: allocated=549184K, used=371473K, max=699072K
  • When the expected client load will exceed the maximum: As a rule of thumb:
    • Smart Client: assume a memory usage of about 600kb for each Smart Client. NOTE: When the Servoy HTTP Tunnel is used, assume 900kb per Client.
    • All other type of Clients (Web, Headless, Batch): the required memory for each client depends on the size and design of the solution, but should be in the order of magnitude of a couple of Mb per Client.
  • When there is plenty of free real memory available on the OS level. Java processes in general perform better when not having memory constraints. For example, when 2Gb free real memory is left, add the 2Gb to the maximum heap size. The Java process will only take what it needs.

Memory Management basics for Java

Java memory consumption is divided in several different area's, called spaces. The most important ones are the Java Heap Space and the Java Perm Space, but there are others.

Each area is by default configured with an initial size and a maximum size, but these sizes can be overruled using arguments when launching the Java process. The defaults values differ per JVM implementation and can depend on the hardware on which the JVM is started. 

The JVM will always respect the maximum size set of a specific space. In the case of the Java process hits the maximum size for a specific space, a relevant exception will be thrown, for example an OutOfMemory or Stack Overflow exception. Thus it's vital to dimension the maximum size of the relevant spaces according to the requirements of the process.  

Java Heap Space

lkdflk

Java Perm Space

kl

Initial Java Heap Size
Maximum Java Heap Size
Maximum Java Perm Size

Servoy Application Server

Smart Client

Initial Client Heap

Max. Client Heap

Virtual Machine Arguments

More reading

...

  • .

...