MapTool Memory Usage: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Added section on modifying the .jnlp file)
(Rewritten to provide more information on how MapTool uses memory; added section on editing .jnlp, .sh, and .command files)
Line 1: Line 1:
==What is the Stack?==
==How Maptool Uses Memory==
When MapTool starts, the Java virtual machine (the program that lets MapTool run on your computer!) configures a number of settings for the program. Three of these settings affect the memory given to MapTool: ''heap memory'' (the memory MapTool uses to store data and resources), and ''stack memory'' (the amount of memory threads are allowed to use).


When MapTool starts, the Java Virtual Machine (the program that lets MapTool run on your computer!) configures a number of settings for the program. Two of these settings involve the available memory given to MapTool: main memory, and the '''stack'''.
===Heap Memory===
The heap memory allocated to MapTool indicates how much memory MapTool uses to store objects (maps, tokens, image files, macros, etc.) Heap memory allocation is controlled by two options: <code>-Xms</code> sets the ''starting heap size'' (the initial amount of memory MapTool is allocated) and <code>-Xmx</code> sets the ''maximum heap size '' (the maximum amount of memory MapTool is allowed to access).


===Main Memory Allocation===
If you set the maximum heap size too low, MapTool may run out of memory and crash, freeze, or have other problems. MapTool clients that are connecting to a server should use the same memory settings as the server when possible; otherwise clients may freeze or lose connection when using larger or more complex maps.


The main memory allocated to MapTool simply indicates how much of your computer's RAM MapTool can play with - if you have, for instance, 2GB of RAM, you can tell MapTool that it can play around with 1GB; likewise if you want, you can say "MapTool can only use 256KB of RAM!"
===Stack Memory===
The stack memory is the amount of memory each ''thread'' is given; threads are sub-processes that handle application functions like network access, macro execution, and drawing the UI. Stack memory allocation is controlled by one option: <code>-Xss</code> sets the ''stack size'' (each thread is given exactly the amount specified; there is no starting or maximum stack size).


===Stack Size===
Some macro frameworks will require larger stack sizes as they perform more complex calculations and functions; they will usually specify their stack requirements. If a thread runs out of stack memory you will see a '''StackOverflow''' error and the macro will not work.


There is another memory amount that Java sets up when MapTool launches, and this one has to do not with the total amount of memory that MapTool gets, but the amount of memory any particular ''thread'' of a program can eat up at a given time. This memory is called the ''stack''.  
==Configuring memory allocation for MapTool==
In the beginning, MapTool used whatever default stack size and heap size Java felt like setting. The actual amount varied from operating system to operating system, but it was generally enough for the simple macros that were in use at the time. As the power and flexibility of the macro code increased, macros began to bump up against the limits of the default stack, and users began adjusting the stack size to compensate. As frameworks and maps became larger and more complex, users began adjusting the maximum heap size.


The stack is especially important when you are using macros to perform calculations: the more complex a macro gets (and some macros can get ''very'' complex!) the more ''stack memory'' it will consume.  
The amount of memory is specified in kilobytes or megabytes; "512K" is 512 kilobytes, while "512M" is 512 megabytes.


Eventually, a macro can get so complex, it eats up all the stack it is permitted, and then cries out for more. When this happens, you get a '''Stack Overflow''' error, and the macro will simply Not Work.
<font color="red">'''WARNING'''</font>: '''Stack memory is allocated in addition to heap memory and ''each thread receives the same amount of stack memory.'' If you set the stack memory allocation too high, the Java VM can consume far more memory than is necessary which will affect overall computer performance.


==Configuring the Stack==
===Setting memory allocation in a batch file===
One of the ways to start MapTool is via the various script or ''batch files'' that are included when you download and unzip a copy of MapTool. There are three different types of script files included with MapTool as of 1.3.v77:


In the beginning, MapTool used whatever default stack size Java felt like setting. The actual amount varied from computer to computer and operating system to operating system, but it was generally enough for the simple macros that were in use at the time.
* <code>Launch MapTool.bat</code>, <code>Launch MapTool-512M-Memory.bat</code> and <code>Launch MapTool-1G-Memory.bat</code> for Windows; the different names refer to different maximum heap allocations
* <code>Launch MapTool.sh</code> for Linux and Mac OS X
* <code>Launch MapTool.command</code> for Mac OS X; this file can be double-clicked to start MapTool.  


As the power and flexibility of the macro code increased, macros began to bump up against the limits of the default stack, and users began adjusting their stack size to compensate.  
====Editing a .bat file====
Though each batch file has different heap sizes specified, the format is the same:
javaw -Xmx512M -Xss512K -jar maptool-*.jar run


===Setting the Stack Size in a .BAT or .SH File===
To set the maximum heap size, change the <code>-Xmx</code> option. To set the stack size, change the <code>-Xss</code> option.  


One of the ways to start MapTool is via the various ''batch files'' that are included when you download and unzip a copy of MapTool. These files are called things like '''Launch Maptool-512M-Memory.bat''' (for Windows machines) and '''Launch MapTool.sh''' (for Linux). The number in the filename indicates the amount of ''main memory'' allocated to MapTool. The contents of these batch files looks like:
====Editing the .sh or .command file====
At the top of the shell script file are three variables:
MAXMEMSZ="768m"
MINMEMSZ="32m"
STACKSZ="2m"


{{code|javaw -Xmx512M -Xss512K -jar maptool-*.jar run}}
You can set the starting heap, maximum heap, and stack size by changing these variables.


The critical pieces here are the ones that begin with {{code|-X}}. The first, {{code|-Xmx512M}} indicates how much main memory (in this case, 512MB) is allocated to the Java virtual machine.  
===Setting the memory allocation using MapToolLauncher===
If you start MapTool with the Windows launcher, you can set the starting heap (Min Mem), maximum heap (Max Mem), and stack  (Stack Size) sizes.  


The second, {{code|-Xss512K}} is how much memory is allocated to the stack (the "ss" is for "stack size").
The Windows launcher defaults to a 2MB stack which is perfectly acceptable for pretty much all uses. If you change the memory values, the new values will be saved for the next launch.
 
In versions of MapTool prior to 1.3.b54, the {{code|-Xss512K}} is absent. To set the stack size allocated (overriding the defaults that are picked by Java or the value indicated), do the following:
 
1. Open the batch file in a text editor like Notepad.
 
2. Edit the {{code|-Xss512K}} line to be another value, for instance, {{code|-Xss1M}} or {{code|-Xss2M}} (M is for megabytes, K is for kilobytes).
 
3. Save the file, and then start MapTool using that batch file.
 
<font color="red">'''WARNING'''</font>: When you are changing the stack size, the best settings are usually 512K, 1M, or 2M. <font color="red">'''DO NOT set it to 512M'''!</font> That will cause Bad Things to happen.
 
===Setting the Stack Size using MapToolLauncher===
 
If you start MapTool with the Windows launcher, the third field is where you can indicate stack size.
 
The Windows launcher defaults to a 2MB stack, which is perfectly acceptable for pretty much all uses. If you change it, that new value will be saved for the next time.
 
===Setting the Stack Size from WebStart===


===Setting the memory allocation for Java WebStart===
If you start MapTool using the Java WebStart option, you can change the settings here, too.  
If you start MapTool using the Java WebStart option, you can change the settings here, too.  


Line 64: Line 61:
         max-heap-size="512m"
         max-heap-size="512m"
         java-vm-args="-Xmx512m -Xms64m -Xss2m"/>
         java-vm-args="-Xmx512m -Xms64m -Xss2m"/>
* To change the starting memory heap size, change the <code>initial-heap-size</code> entry and the <code>-Xms</code> entry
* To change the starting heap size, change the <code>initial-heap-size</code> entry and the <code>-Xms</code> entry
* To change the maximum memory heap size, change the <code>max-heap-size</code> entry and the <code>-Xmx</code> entry
* To change the maximum heap size, change the <code>max-heap-size</code> entry and the <code>-Xmx</code> entry
* To change the stack memory size, change the <code>-Xss</code> entry
* To change the stack memory size, change the <code>-Xss</code> entry
* Save the .jnlp file
* Save the .jnlp file


===Setting the Stack Size in Mac OSX===
===Setting the memory allocation in the Mac OS X application bundle===
 
Users who download the ''.dmg'' (disk image) file from the [http://www.rptools.net RPTools web site] and installed MapTool from it will see MapTool as a Mac OS X application bundle. MapTool can be launched by double-clicking it like any other Mac OS application.
These instructions apply to OSX users who have downloaded the '''.dmg''' file from the [http://www.rptools.net RPTools web site] and installed MapTool from it. They were most recently tested on '''Snow Leopard 10.6.3'''. OSX users who are running MapTool using the WebStart approach won't need these instructions since the CUSTOMIZATION link on the [http://www.rptools.net/index.php?page=launch Launch page] allows the memory settings to be configured there.
 
(Unfortunately, Apple doesn't make this process easy. Their belief seems to be that the application provider should allow these values to be changed from inside the application but they didn't consider that the Java memory settings cannot be changed from inside Java. )
 
The installer in the .dmg creates what Apple calls an "app" -- it's a directory with the .app filename extension that contains all of the files necessary for the application. This includes the configuration file when the program is Java-based.
 
Here's the procedure for editing the configuration file (called a property list). Note that I'm describing the technique that uses the OSX TextEdit program because the only property list editor I have on my machine is under the /Developer directory -- which is an optional install for OSX so most people won't have it.


# Ctrl-click (or right-click) on the application icon (the filename should end with .app).
* Ctrl-click (or right-click) on MapTool application icon.
# Choose Show Package Contents and a Finder window will open.
* Choose "Show Package Contents" and a new Finder window will open.
# Open the Contents folder and locate the Info.plist file.
* Open the Contents folder and locate the ''Info.plist'' file.
# Ctrl-click (or right-click) on that file and choose Open With > Other...
* Ctrl-click (or right-click) on that file and choose Open With > Other...
# In the Choose Application dialog that opens, locate TextEdit and double-click on it.
* In the Choose Application dialog that opens, locate TextEdit and double-click on it.
# The Info.plist file is now open. There is an entry in the file called VMOptions (mine was about two-thirds of the way down the file) that should be changed to match the memory characteristics your GM wants you to use. Here's what mine looks like in case you want a reference for the following steps:
* Look for the following section:
#: <key>VMOptions</key>
    <key>VMOptions</key>
#:    <array>
      <array>
#:         <string>-Xmx768m</string>
         <string>-Xmx768m</string>
#:         <string>-Xms64m</string>
         <string>-Xms64m</string>
#:         <string>-Xss3m</string>
         <string>-Xss3m</string>
#:      </array>
      </array>
# Replace your entries with the example text in Step 6. However, your GM may have directed you to use a particular value for the "maximum memory" setting. If so, replace the 768 number with the new maximum. The GM may also ask you to change the "stack size" amount -- that's the 3 number following the ss. (The 64 number never needs to be changed.) If you have other entries between the <array> and </array> lines, they should be removed because you're specifying completely new values.
# Use Cmd-Q and then choose Save. You can close the Finder window that opened as well.


You're all done!
* To set the starting heap size, change the <code>-Xms</code> option. To set the maximum heap size, change the <code>-Xmx</code> option. To set the stack size, change the <code>-Xss</code> option.
* Save the file. The next time you double-click the MapTool icon, it will launch using the new memory settings.


[[Category:MapTool]]
[[Category:MapTool]]

Revision as of 03:57, 24 December 2010

How Maptool Uses Memory

When MapTool starts, the Java virtual machine (the program that lets MapTool run on your computer!) configures a number of settings for the program. Three of these settings affect the memory given to MapTool: heap memory (the memory MapTool uses to store data and resources), and stack memory (the amount of memory threads are allowed to use).

Heap Memory

The heap memory allocated to MapTool indicates how much memory MapTool uses to store objects (maps, tokens, image files, macros, etc.) Heap memory allocation is controlled by two options: -Xms sets the starting heap size (the initial amount of memory MapTool is allocated) and -Xmx sets the maximum heap size (the maximum amount of memory MapTool is allowed to access).

If you set the maximum heap size too low, MapTool may run out of memory and crash, freeze, or have other problems. MapTool clients that are connecting to a server should use the same memory settings as the server when possible; otherwise clients may freeze or lose connection when using larger or more complex maps.

Stack Memory

The stack memory is the amount of memory each thread is given; threads are sub-processes that handle application functions like network access, macro execution, and drawing the UI. Stack memory allocation is controlled by one option: -Xss sets the stack size (each thread is given exactly the amount specified; there is no starting or maximum stack size).

Some macro frameworks will require larger stack sizes as they perform more complex calculations and functions; they will usually specify their stack requirements. If a thread runs out of stack memory you will see a StackOverflow error and the macro will not work.

Configuring memory allocation for MapTool

In the beginning, MapTool used whatever default stack size and heap size Java felt like setting. The actual amount varied from operating system to operating system, but it was generally enough for the simple macros that were in use at the time. As the power and flexibility of the macro code increased, macros began to bump up against the limits of the default stack, and users began adjusting the stack size to compensate. As frameworks and maps became larger and more complex, users began adjusting the maximum heap size.

The amount of memory is specified in kilobytes or megabytes; "512K" is 512 kilobytes, while "512M" is 512 megabytes.

WARNING: Stack memory is allocated in addition to heap memory and each thread receives the same amount of stack memory. If you set the stack memory allocation too high, the Java VM can consume far more memory than is necessary which will affect overall computer performance.

Setting memory allocation in a batch file

One of the ways to start MapTool is via the various script or batch files that are included when you download and unzip a copy of MapTool. There are three different types of script files included with MapTool as of 1.3.v77:

  • Launch MapTool.bat, Launch MapTool-512M-Memory.bat and Launch MapTool-1G-Memory.bat for Windows; the different names refer to different maximum heap allocations
  • Launch MapTool.sh for Linux and Mac OS X
  • Launch MapTool.command for Mac OS X; this file can be double-clicked to start MapTool.

Editing a .bat file

Though each batch file has different heap sizes specified, the format is the same:

javaw -Xmx512M -Xss512K -jar maptool-*.jar run

To set the maximum heap size, change the -Xmx option. To set the stack size, change the -Xss option.

Editing the .sh or .command file

At the top of the shell script file are three variables:

MAXMEMSZ="768m"
MINMEMSZ="32m"
STACKSZ="2m"

You can set the starting heap, maximum heap, and stack size by changing these variables.

Setting the memory allocation using MapToolLauncher

If you start MapTool with the Windows launcher, you can set the starting heap (Min Mem), maximum heap (Max Mem), and stack (Stack Size) sizes.

The Windows launcher defaults to a 2MB stack which is perfectly acceptable for pretty much all uses. If you change the memory values, the new values will be saved for the next launch.

Setting the memory allocation for Java WebStart

If you start MapTool using the Java WebStart option, you can change the settings here, too.

  1. Go to The Launch Page
  2. Click the CUSTOMIZATION link.
  3. Enter the values you want to use when you WebStart one of the applications.
  4. Click on the links above to start the application you wish to use.

If you have a saved .jnlp (WebStart) file, you can edit the memory settings by hand.

  • Open the .jnlp file in a text editor
  • Look for the following section (the actual memory values may differ in your copy):
   <j2se version="1.5+"
       initial-heap-size="64m"
       max-heap-size="512m"
       java-vm-args="-Xmx512m -Xms64m -Xss2m"/>
  • To change the starting heap size, change the initial-heap-size entry and the -Xms entry
  • To change the maximum heap size, change the max-heap-size entry and the -Xmx entry
  • To change the stack memory size, change the -Xss entry
  • Save the .jnlp file

Setting the memory allocation in the Mac OS X application bundle

Users who download the .dmg (disk image) file from the RPTools web site and installed MapTool from it will see MapTool as a Mac OS X application bundle. MapTool can be launched by double-clicking it like any other Mac OS application.

  • Ctrl-click (or right-click) on MapTool application icon.
  • Choose "Show Package Contents" and a new Finder window will open.
  • Open the Contents folder and locate the Info.plist file.
  • Ctrl-click (or right-click) on that file and choose Open With > Other...
  • In the Choose Application dialog that opens, locate TextEdit and double-click on it.
  • Look for the following section:
   <key>VMOptions</key>
     <array>
       <string>-Xmx768m</string>
       <string>-Xms64m</string>
       <string>-Xss3m</string>
     </array>
  • To set the starting heap size, change the -Xms option. To set the maximum heap size, change the -Xmx option. To set the stack size, change the -Xss option.
  • Save the file. The next time you double-click the MapTool icon, it will launch using the new memory settings.