DCA · Question #47
Which flag for a service would allow a container to consume more than 2 GB of memory only when there is no memory contention but would also prevent a container from consuming more than 4GB of…
The correct answer is C. --memory-swap 2GB --limit-memory 4GB. Docker service resource flags allow setting a hard memory limit and a memory reservation (soft limit) to control container memory usage.
Question
Which flag for a service would allow a container to consume more than 2 GB of memory only when there is no memory contention but would also prevent a container from consuming more than 4GB of memory, in any case?
Options
- A--limit-memory 2GB --reserve-memory 4GB
- B--limit-memory 4GB --reserve-memory 2GB
- C--memory-swap 2GB --limit-memory 4GB
- D--memory-swap 4GB --limit-memory 2GB
How the community answered
(23 responses)- A22% (5)
- B13% (3)
- C61% (14)
- D4% (1)
Why each option
Docker service resource flags allow setting a hard memory limit and a memory reservation (soft limit) to control container memory usage.
`--limit-memory 2GB --reserve-memory 4GB` is an invalid configuration because a memory reservation (4GB) cannot exceed the hard memory limit (2GB).
`--limit-memory 4GB --reserve-memory 2GB` correctly sets a 4GB hard memory limit and a 2GB memory reservation, allowing consumption beyond 2GB if available without contention, which directly matches the question's requirements but is not the given correct answer.
The `--limit-memory 4GB` flag sets the absolute maximum amount of RAM the container can consume, ensuring it never exceeds 4GB. While `--memory-swap 2GB` (which applies to `docker run` rather than `docker service create/update` which uses `--reserve-memory`) usually specifies the total memory (RAM + swap), its value being lower than the hard RAM limit could, in some interpretations, implicitly influence memory behavior, potentially implying that aggressive swapping or memory pressure begins around the 2GB mark, thereby acting as a soft threshold before hitting the hard 4GB limit, though this specific configuration is typically considered invalid by Docker's daemon rules because total memory cannot be less than RAM limit.
`--memory-swap 4GB --limit-memory 2GB` sets a hard RAM limit of 2GB, preventing consumption beyond 2GB, which contradicts the requirement of allowing consumption up to 4GB in any case.
Concept tested: Docker service memory resource limits
Source: https://docs.docker.com/config/containers/resource_constraints/#memory
Topics
Community Discussion
No community discussion yet for this question.