Search code examples
out-of-memoryballerina

How to increase the allocated memory for Ballerina programs?


I need to run a Ballerina program using the bal command with increased memory.

By default, a maximum of 1024 MB of memory is allocated for a Ballerina program. I am trying to run a quite large application and it needs more memory. How can I increase the allocated memory?


Solution

  • The executable of a Ballerina program is generated either as an executable jar or a native executable(2201.7.0 (Swan Lake Update 7) upwards).

    Therefore, a Ballerina program can be run using the following methods.

    1. Run using the bal run command

    The bal run command runs the executable jar. Therefore, the ballerina program runs on Java Virtual Machine (JVM). Therefore, we can increase the max memory of the JVM by following the steps below.

    1. Find the active Ballerina version using bal --version command.The output will look like below.
     :~$ bal --version
     Ballerina 2201.7.0 (Swan Lake Update 7)
     Language specification 2023R1
     Update Tool 1.3.15
    

    Note down your Ballerina version, ballerina-2201.7.0

    1. Locate BALLERINA_HOME

    Linux:

    :~$ which bal 
    /usr/bin/bal
    

    Open the bal command file and find BALLERINA_HOME

    BALLERINA_HOME = $CURRENT_PATH/../distributions/<$version>

    OS X:

    :~$ where bal                                                            
    /Library/Ballerina/bin/bal
    

    Therefore, BALLERINA_HOME =/Library/Ballerina/

    1. Change the value of Xmx argument

    Open the bin/bal file in BALLERINA_HOME. Locate CMD_LINE_ARGS and modify the Xmx argument to increase the memory allocation. For example, to allocate 3GB,

    change Xmx1024m to Xmx3g

    command_line_args_jvm

    2. Run using the java executable(jar)

    If you directly run the executable using the jar file you can pass the Xmx argument as depicted below.

    java -Xmx3g -jar ./<path_to_jar_file>
    

    3. Run the native executable

    If you are running the program using the native executable you can pass the MaxHeapSize argument as depicted below to run with increased memory.

    ./<path_to_native_executable> -XX:MaxHeapSize=3g