How to: OOM behaviour
Sometimes you may encounter OOM (Out of Memory) issue.  The issues vary in logs, while you could find the essence by checking the exit code (`echo $?`).

For demonstration, below examples try to execute [MemoryFiller](https://github.com/thyrlian/AndroidSDK/blob/master/misc/MemoryFiller/MemoryFiller.java) which can fill memory up quickly.

* **Exit Code** `137` (= 128 + 9 = SIGKILL = Killed)

  Example code:
  
    ```bash
    # spin up a container with memory limit (128MB)
    docker run -it -m 128m -v $(pwd)/misc/MemoryFiller:/root/MemoryFiller thyrlian/android-sdk /bin/bash
    # fill memory up
    cd /root/MemoryFiller && javac MemoryFiller.java
    java MemoryFiller
    ```

  Logs:
  
    ```console
    Killed
    ```

  Commentary: The process was in extreme resource starvation, thus was killed by the kernel OOM killer.  This happens when **JVM max heap size > actual container memory**.  Similarly, the logs could look like this when running a gradle task in an Android project: `Process 'Gradle Test Executor 1' finished with non-zero exit value 137`.

* **Exit Code** `1` (= SIGHUP = Hangup)

  Example code:
  
    ```bash
    # spin up a container with memory limit (or without - both lead to the same result)
    docker run -it -m 128m -v $(pwd)/misc/MemoryFiller:/root/MemoryFiller thyrlian/android-sdk /bin/bash
    # fill memory up
    # enable Docker memory limits transparency for JVM
    cd /root/MemoryFiller && javac MemoryFiller.java
    java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap MemoryFiller
    ```

  Logs:
  
    ```console
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at MemoryFiller.main(MemoryFiller.java:13)
    ```

  Commentary: With [enabling Docker memory limits transparency for JVM](https://blogs.oracle.com/java-platform-group/java-se-support-for-docker-cpu-and-memory-limits), JVM is able to correctly estimate the max heap size, and it won't be killed by the kernel OOM killer any more.  Similarly, the logs could look like this when running a gradle task in an Android project: `Process 'Gradle Test Executor 1' finished with non-zero exit value 1`.  In this case, you should either check your code or tweak your memory limit for container (or JVM heap parameters, or even the host memory size).

* **Exit Code** `3` (= SIGQUIT = Quit)

  Example code:
  
    ```bash
    # spin up a container without memory limit
    docker run -it -v $(pwd)/misc/MemoryFiller:/root/MemoryFiller thyrlian/android-sdk /bin/bash
    # fill memory up
    cd /root/MemoryFiller && javac MemoryFiller.java
    # make sure that Docker memory resource is big enough > JVM max heap size
    # otherwise it's better to run with UnlockExperimentalVMOptions & UseCGroupMemoryLimitForHeap enabled
    java -XX:+ExitOnOutOfMemoryError MemoryFiller
    ```

  Logs:
  
    ```console
    Terminating due to java.lang.OutOfMemoryError: Java heap space
    ```

  Commentary: JRockit JVM exits on the first occurrence of an OOM error. It can be used if you prefer restarting an instance of JRockit JVM rather than handling OOM errors.

* **Exit Code** `134` (= 128 + 6 = SIGABRT = Abort)

  Example code:
  
    ```bash
    # spin up a container without memory limit
    docker run -it -v $(pwd)/misc/MemoryFiller:/root/MemoryFiller thyrlian/android-sdk /bin/bash
    # fill memory up
    cd /root/MemoryFiller && javac MemoryFiller.java
    # make sure that Docker memory resource is big enough > JVM max heap size
    # otherwise it's better to run with UnlockExperimentalVMOptions & UseCGroupMemoryLimitForHeap enabled
    java -XX:+CrashOnOutOfMemoryError MemoryFiller
    ```

  Logs:
  
    ```console
    Aborting due to java.lang.OutOfMemoryError: Java heap space
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  Internal Error (debug.cpp:308), pid=63, tid=0x00007f208708d700
    #  fatal error: OutOfMemory encountered: Java heap space
    #
    # JRE version: OpenJDK Runtime Environment (8.0_131-b11) (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
    # Java VM: OpenJDK 64-Bit Server VM (25.131-b11 mixed mode linux-amd64 compressed oops)
    # Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
    #
    # An error report file with more information is saved as:
    # /root/MemoryFiller/hs_err_pid63.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://bugreport.java.com/bugreport/crash.jsp
    #
    Aborted
    ```

  Commentary: JRockit JVM crashes and produces text and binary crash files when an OOM error occurs.  When JVM crashes with a fatal error, an error report file `hs_err_pid***.log` will be generated in the same working directory.