Memory and container sizing in Hadoop - Time & Space Complexity
We want to understand how the size of memory and containers affects the time it takes for Hadoop jobs to run.
How does changing memory or container size change the work done over time?
Analyze the time complexity of the following Hadoop container allocation snippet.
// Request container with specific memory
Configuration conf = new Configuration();
conf.setInt("yarn.scheduler.maximum-allocation-mb", 4096);
conf.setInt("mapreduce.map.memory.mb", 2048);
conf.setInt("mapreduce.reduce.memory.mb", 4096);
// Launch container with requested memory
ContainerLaunchContext ctx = ContainerLaunchContext.newInstance();
// Container runs map or reduce task
This code sets memory sizes for containers that run map and reduce tasks in Hadoop.
Look at what repeats during job execution.
- Primary operation: Running map and reduce tasks inside containers.
- How many times: Once per task, repeated for all tasks in the job.
When input data grows, more tasks run, so more containers launch.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 containers launched |
| 100 | 100 containers launched |
| 1000 | 1000 containers launched |
Pattern observation: Operations grow roughly linearly with input size because each input chunk needs a container.
Time Complexity: O(n)
This means the total work grows in direct proportion to the number of input splits or tasks.
[X] Wrong: "Increasing container memory always makes the job run faster."
[OK] Correct: More memory per container can reduce parallelism if total cluster memory is fixed, causing longer total time.
Understanding how container memory sizing affects job time helps you design efficient Hadoop jobs and shows you can think about resource use and scaling.
"What if we doubled the container memory but kept total cluster memory the same? How would the time complexity change?"
Practice
Solution
Step 1: Understand container memory role and compare options
Container memory size limits how much RAM a Hadoop task can use while running. Only To limit the amount of memory a task can use during execution correctly describes this purpose; others relate to disk, CPU, or network, which are unrelated.Final Answer:
To limit the amount of memory a task can use during execution -> Option DQuick Check:
Container memory = task RAM limit [OK]
- Confusing memory with disk space
- Thinking container memory controls CPU cores
- Assuming it sets network bandwidth
Solution
Step 1: Understand heap size vs container memory and check options
Java heap size (-Xmx) must be less than container memory to avoid errors. mapreduce.map.memory.mb=2048\nmapreduce.map.java.opts=-Xmx1536m sets container memory to 2048 MB and heap to 1536 MB, which is valid. Others have heap larger than container memory, causing errors.Final Answer:
mapreduce.map.memory.mb=2048\nmapreduce.map.java.opts=-Xmx1536m -> Option BQuick Check:
Heap size < container memory [OK]
- Setting heap size larger than container memory
- Using inconsistent units (MB vs GB)
- Forgetting to set Java opts after memory
mapreduce.reduce.memory.mb=4096 mapreduce.reduce.java.opts=-Xmx3072m
What will happen if a reduce task tries to use 3500 MB heap memory?
Solution
Step 1: Compare requested heap with limits
Heap limit is 3072 MB, but task tries 3500 MB, which exceeds Java heap limit (-Xmx). Container memory is 4096 MB, so 3500 MB fits container but exceeds heap limit, causing failure.Final Answer:
The task will fail due to exceeding Java heap size limit -> Option CQuick Check:
Heap > Java heap limit = failure [OK]
- Confusing container memory with Java heap size
- Assuming container memory limits heap directly
- Thinking container memory auto-adjusts
mapreduce.map.memory.mb=1024 mapreduce.map.java.opts=-Xmx1536m
Solution
Step 1: Compare Java heap size and container memory
Heap size (-Xmx1536m) is 1536 MB, which is larger than container memory (1024 MB). This causes the task to fail because heap cannot exceed container memory.Final Answer:
Java heap size is larger than container memory size -> Option AQuick Check:
Heap > container memory = error [OK]
- Ignoring heap size limits
- Assuming syntax error instead of sizing error
- Thinking container memory can auto-expand
Solution
Step 1: Calculate container memory with overhead
Container memory must be larger than Java heap size to allow overhead (usually 25-30%). Heap = 3 GB, so container memory should be at least 3 GB + 25% = 3.75 GB, rounded to 4 GB.Final Answer:
4 GB -> Option AQuick Check:
Container memory > heap size + overhead [OK]
- Setting container memory equal to heap size
- Ignoring overhead memory needs
- Confusing total job memory with per-task memory
Ready to prove it?
Pass the test and this pattern moves unknown
