User Tools

Site Tools


diverse:commandline:estimatemem

Estimate Memory Requirements

Author

Jonathan Jackson (jjackson a.t familyjackson dot net)

Description

This is a short script to estimate the java heap size to use based on the size of images on disk. It prints a warning and exits if there is not enough RAM available on the machine.

Supported Systems

estimate_mem has been tested on Linux but should work on MacOS X / Solaris / Cygwin

Installation

Save the 'estimate_mem' script (to ImageJ/bin) and make the file executable etc.

#!/bin/bash 
 
authors='Jon Jackson'
email='j.jackson |at| ucl.ac.uk'
version='2008-12-04'
 
function usage {
    echo "Usage: $(basename $0) [options] imagefile"
    echo " estimates memory required to process imagefile with imagej library"
    echo " options: "
    echo "    -b <value>    base memory value (integer)" 
    echo "    -m <value>    multiply by value (integer)"  
    echo ""
    echo "Version: $version. Author(s): $authors (contact: $email)"
    echo ""
}
 
declare -i im_file_size
declare -i mem=512
declare -i min_mem=32
declare -i max_mem
declare -i max_32bit=1800
declare -i max_64bit=1000000 # This figure can be increased up to upper limit for 64bit OS 
shopt -s extglob
 
declare -i multiply=1
declare -i basemem=64
 
while getopts b:Dm: options
do
    case $options in
        b) basemem=${OPTARG}
            if (( "$basemem" <= 0 )) ; then
                echo "$(basename $0): invalid value for -m: $OPTARG" 1>&2
                echo "Positive nteger value required" 1>&2
                usage; exit 1 
            fi       
            ;;
        D) debug='true'
            ;;
        m) multiply=${OPTARG}
            if (( "$multiply" <= 0 )) ; then
                echo "$(basename $0): invalid value for -m: $OPTARG" 1>&2
                echo "Positive nteger value required" 1>&2
                usage; exit 1 
            fi
            ;;        
        \?) usage
            exit 1 ;;
    esac
done
 
declare -i i=1
while (( i < $OPTIND )) ; do
    shift
    i=i+1
done
 
if (( "${#*}" == 0 )) ; then
    usage
    exit 1
fi
 
im_file="$1"
 
# Estimate memory required based on file size
 
if [[ -f "$im_file" ]] ; then
    if [[ "$im_file" == *zip ]] ; then
        im_file_size=$(unzip -l "$im_file" | awk '{mem=$1} END{print mem}')
    elif [[ "$im_file" == *@(bz2|gz|tgz) ]] ; then
        echo "Warning: memory requirements are not yet estimated correctly for compressed files ($im_file)" 
    else
        im_file_size=$(stat -L -c "%s" $(echo $im_file | sed 's/.hdr$/.img/') )
    fi
    if (( $im_file_size == 0 )) ; then
        echo "Unable to estimate file size for $im_file"
    else
        # estimate memory required for 'multiply' x file of this size (INTEGER OPERATIONS ONLY)
        mem="$im_file_size/1000000*${multiply}+${basemem}"
    fi
fi
 
if [[ `uname` == 'SunOS' ]] ; then
    max_mem=`vmstat | awk 'BEGIN{maxMem='$max_64bit'} NR == 3 {fmem=int($5 / 1024); if (fmem < maxMem) {print fmem} else {print maxMem}}'`
elif [[ `uname` == 'Linux' ]] ; then
    if [[ `uname -m` == 'x86_64' ]] ; then
        max_mem=`free | awk -v maxMem=$max_64bit 'NR == 2 {fmem=int($2 / 1024); if (fmem < maxMem) {print fmem} else {print maxMem}}'`
    else
        max_mem=`free | awk -v maxMem=$max_32bit 'NR == 2 {fmem=int($2 / 1024); if (fmem < maxMem) {print fmem} else {print maxMem}}'`
    fi
fi
if (( $mem > $max_mem )) ; then
    echo "Sorry, there is not enough RAM memory available on this machine"
    echo "Required: $mem MB; available: $max_mem MB"
    echo "`basename $0` is EXITING"
    exit 1
fi
if (( $mem < $min_mem )) ; then mem=$min_mem ; fi
 
echo $mem
diverse/commandline/estimatemem.txt ยท Last modified: 2019/04/12 13:13 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki