CPP Memory Management & Performance Optimization 1 ā Questions and Answers
Question 1: What is a memory leak in software applications?
- Memory that is accessed too quickly
- Allocated memory that is never freed, causing the program to consume increasing amounts of RAM (Correct answer)
- Memory shared between two processes
- A CPU cache miss
Correct answer: Allocated memory that is never freed, causing the program to consume increasing amounts of RAM
A memory leak occurs when a program allocates heap memory but never releases it, eventually exhausting available memory.
Question 2: What is garbage collection in a managed runtime environment?
- Manual deallocation of heap memory by the programmer
- Automatic identification and freeing of memory no longer referenced by the application (Correct answer)
- Compressing unused disk space
- Clearing CPU caches periodically
Correct answer: Automatic identification and freeing of memory no longer referenced by the application
Garbage collection automates memory reclamation by detecting objects with no live references and freeing their memory.
Question 3: What is the difference between stack and heap memory?
- Stack is slower; heap is faster
- Stack memory is automatically managed and stores local variables; heap memory is dynamically allocated and manually managed (Correct answer)
- Stack stores objects; heap stores primitives
- They are two names for the same memory region
Correct answer: Stack memory is automatically managed and stores local variables; heap memory is dynamically allocated and manually managed
The stack holds local variables with automatic allocation/deallocation per function call, while the heap holds dynamically allocated objects.
Question 4: What does 'Big O notation' measure in algorithm performance?
- Exact execution time in milliseconds
- Memory address size
- How an algorithm's runtime or space requirements grow relative to input size (Correct answer)
- The number of CPU cores used
Correct answer: How an algorithm's runtime or space requirements grow relative to input size
Big O notation describes the upper bound of an algorithm's time or space complexity as a function of input size.
Question 5: What is CPU cache locality and why does it matter for performance?
- Storing data on the same CPU core to avoid network latency
- Accessing data that is physically close in memory, improving cache hit rates and reducing access time (Correct answer)
- Pinning threads to specific CPU cores
- Preloading all application data at startup
Correct answer: Accessing data that is physically close in memory, improving cache hit rates and reducing access time
Cache locality means accessing contiguous or nearby memory addresses so data is likely already in the fast CPU cache, reducing costly RAM fetches.
Question 6: What is object pooling as a performance optimization technique?
- Sharing database connection strings
- Reusing a set of pre-allocated objects instead of repeatedly creating and destroying them (Correct answer)
- Pooling CPU threads into a fixed-size group
- Combining small objects into one large object
Correct answer: Reusing a set of pre-allocated objects instead of repeatedly creating and destroying them
Object pooling reduces garbage collection pressure and allocation overhead by recycling expensive-to-create objects.
What is a memory leak in software applications?