JVM Runtime.getRuntime().availableProcessors() returns 1 on AWS ECS Fargate

1.5k views Asked by At
public class Example {
   public static void main(String[] args) {
      // print statement at the start of the program
      System.out.println("Start...");
      System.out.print("Number of available processors are: ");
      System.out.println( Runtime.getRuntime().availableProcessors());
   }
}

Ref: https://www.tutorialspoint.com/get-number-of-available-processors-in-java

When I compile and run this code on AWS ECS Fargate getting "1" as the output even though I have specified more than 1 cpu in task def(4098 for 4 vCPU).

Tested Java versions:

  • Corretto-17.0.0.35.1
  • Corretto-8.275.01.1

But when I do the same on AWS EC2 inside docker by allocating CPU with --cpus for "docker run" I get the exact number what I pass to --cpus. Same results with AWS EC2 with Containerd as well.

Also tested this in EKS 1.18 by setting CPU limit. It returns the number of CPUs specified in the CPU limit.

So, wondering why "1" only in AWS ECS.

1

There are 1 answers

1
Parsifal On BEST ANSWER

I'm going to assume that you used the Console to create your task definitions, because I saw the same behavior (and didn't expect it). The documentation says this:

Although you can also specify CPU and memory at the container level for Fargate tasks, this is optional. Most use cases are satisfied by only specifying these resources at the task level

I first created a task definition that specified CPU/memory at the task level. When I ran it, I saw the same output as you, and this is what the task definition looked like (irrelevant information omitted):

    "taskDefinition": {
        "containerDefinitions": [
            {
                "name": "example",
                "image": ".../example:latest",
                "cpu": 0,
            }
        ],
        "cpu": "4096",
        "memory": "8192",
    }

Note that the container CPU is 0.

I then edited the CPU in the container definition, and saw the expected number of available processors when I ran the task. When I retrieved the task definition, it looked like this:

    "taskDefinition": {
        "containerDefinitions": [
            {
                "name": "example",
                "image": ".../example:latest",
                "cpu": 4096,
            }
        ],
        "cpu": "4096",
        "memory": "8192",
    }

I haven't tried creating a task definition using CloudFormation or Terraform to see if it's any different. I have send feedback on the ECS Console.