CalcMem

Script: calcmem.sh Type: Memory Management Utility Version: v1.19

Calculates available system memory in megabytes using /proc/meminfo parsing and ulimit checks. Parses Java memory parameters (Xmx/Xms) and runtime flags. Used internally by BBTools scripts to determine memory allocation via freeRam() and parseXmx() functions.

Basic Usage

calcmem.sh [parameters]

This script is called internally by other BBTools scripts via calcmem.sh parsing functions. When executed directly, it runs the freeRam() function to calculate available memory and parseXmx() to process Java flags.

Parameters

The parseXmx() function processes command-line arguments using pattern matching to extract Java memory flags and runtime parameters.

Memory Parameters

Xmx=<size>
Sets maximum Java heap memory size. Accepts values like '8g', '4096m', or '4194304k'. Can be specified with or without leading dash (-Xmx).
xmx=<size>
Alternative lowercase format for maximum heap memory. Equivalent to Xmx parameter.
Xms=<size>
Sets initial Java heap memory size. If only Xmx is specified, Xms is automatically set to the same value for consistent performance.
xms=<size>
Alternative lowercase format for initial heap memory. Equivalent to Xms parameter.

Java Runtime Parameters

ea
Enable Java assertions. Useful for debugging and development but may impact performance in production.
da
Disable Java assertions. Default behavior for production runs to maximize performance.
ExitOnOutOfMemoryError
Enables -XX:+ExitOnOutOfMemoryError JVM flag. Forces JVM to exit immediately when OutOfMemoryError occurs instead of attempting recovery. Can also be specified as 'exitonoutofmemoryerror' or 'eoom'.

Output and Performance Parameters

json
Output memory information in JSON format. Can be specified as 'json=t', 'json=true', or 'format=json'.
silent
Suppress informational output during memory calculation. Can be specified as 'silent=t' or 'silent=true'.
simd
Enable SIMD (Single Instruction, Multiple Data) vectorization support by adding '--add-modules jdk.incubator.vector' to Java command line. Improves performance on compatible processors. Can be specified as 'simd=t' or 'simd=true'.

Environment Variables

RQCMEM
Manual memory override in megabytes. When set to a value greater than 0, forces memory calculation to use this value instead of system detection.
SLURM_MEM_PER_NODE
Automatically detected SLURM memory allocation per node in megabytes. Used on SLURM-managed clusters to respect job memory limits.

Examples

Memory Calculation

# Calculate available memory (typically called by other scripts)
calcmem.sh

# With specific heap size
calcmem.sh Xmx=8g

# Enable assertions and JSON output
calcmem.sh ea json

These examples show direct usage, though calcmem.sh is usually invoked internally by other BBTools scripts.

Integration with BBTools

# BBTools scripts automatically use calcmem.sh
bbduk.sh in=reads.fq out=clean.fq  # calcmem.sh calculates available memory

# Override memory manually
bbduk.sh in=reads.fq out=clean.fq Xmx=16g  # calcmem.sh processes this setting

Most BBTools scripts source calcmem.sh to calculate memory allocation via freeRam() and parse Java flags via parseXmx().

Algorithm Details

Memory Detection Strategy

CalcMem implements a multi-source memory detection system using the freeRam() function with these specific data sources:

System Memory Sources

Memory Calculation Logic

The script implements memory selection using conditional logic in lines 159-191:

  1. Dual-source comparison: If both vfree > 0 and pfree > 0, selects x2 = min(vfree, pfree)
  2. Scheduler override: If SLURM_MEM_PER_NODE > 0 and (x2 > slurm_x or x2 == 0), forces x = slurm_x
  3. Limit enforcement: If ulimit != "unlimited" and ulimit < x2, sets x = x2
  4. Final calculation: RAM = ((x - 500000) * mult / 100) / 1024, where mult = 84% by default
  5. Fallback value: When x < 1, uses defaultMem = 3200000 KB (3.2 GB)

Environment-Specific Configuration

The setEnvironment() function (lines 73-102) configures PATH variables based on environment detection:

Parameter Processing

The parseXmx() function (lines 11-71) uses pattern matching to handle argument variations:

Technical Notes

Memory Unit Conversion

The freeRam() function (lines 112-125) implements unit parsing using case statements:

Platform Compatibility

Memory Safety Parameters

The final calculation RAM = ((x - 500000) * 84 / 100) / 1024 implements two safety mechanisms:

Support

For questions and support: