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.
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:
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):
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:
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.