title: JVM memory settings in docker container in AWS beanstalk
body: <p>I run my java application in a docker container. I use AWS Beanstalk. The docker base image is CentOS. I run a container on an EC2 instance with 4gb of RAM on Amazon Linux AMI for Beanstalk.</p>
  
  <p>How should I configure the container and JVM memory settings. 
  right now I have:</p>
  
  <p>4GB on Amazon Linux Beanstalk AMI ec2 instance</p>
  
  <p>I dedicated 3GB of 4 for a docker container</p>
  
  <pre><code>{
    "AWSEBDockerrunVersion": 2,
    "Authentication": {
      "Bucket": "elasticbeanstalk-us-east-1-XXXXXX",
      "Key": "dockercfg"
    },
    "containerDefinitions": [
      {
        "name": "my-service",
        "image": "docker-registry:/myapp1.0.2",
        "essential": true,
        "memory": 3184,
        "portMappings": [
          {
            "hostPort": 80,
            "containerPort": 8080
          }
        ]
      }
  }
  </code></pre>
  
  <p>JVM settings are</p>
  
  <pre><code>  -Xms2560m
    -Xmx2560m
    -XX:+UseConcMarkSweepGC
    -XX:+CMSParallelRemarkEnabled
    -XX:+UseCMSInitiatingOccupancyOnly
    -XX:CMSInitiatingOccupancyFraction=70
    -XX:+ScavengeBeforeFullGC
    -XX:+CMSScavengeBeforeRemark
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:HeapDumpPath=./heapdump.hprof
  </code></pre>
  
  <p>Can I dedicate whole ec2 instance memory for a docker container, 4gb, and set JVM to 4GB too?</p>
  
  <p>I think JVM could crash when it is not able to allocate whatever the Xmx memory param says. If so what are the most optimal values I could use for the container and JVM? </p>
  
  <p>Right now I left 1gb margin for Amazon linux itself and 512MB for CentOS running in a container. 1.5GB <code>wasted</code>. Can it be done better?</p>