Memory Leaks from Improper Scoping Quiz Quiz

Explore the fundamentals of memory leaks caused by improper scoping with targeted questions designed to enhance your understanding of variable lifespan, closures, and cleanup techniques. This quiz helps developers and learners recognize common pitfalls in memory management and how to avoid resource leaks in software applications.

  1. Global Variable Leak Scenario

    When a developer unintentionally creates a global variable inside a function by omitting the declaration keyword, how can this lead to a memory leak in a long-running web application?

    1. The variable is only accessible within the function, so it doesn't cause leaks.
    2. The variable is automatically garbage collected after the function ends.
    3. The variable throws a syntax error and halts memory allocation.
    4. The variable remains accessible and is never cleared, occupying memory unnecessarily.

    Explanation: When a variable is created globally due to a missing declaration keyword, it persists for the application's lifetime, potentially causing a memory leak as it is never deallocated. The automatic garbage collection only happens for variables with proper scoping; option B is incorrect because these variables linger. Option C is incorrect, as such declarations do not throw syntax errors. Option D misunderstands scope rules since global variables are not limited to the function scope.

  2. Closures and Leaked References

    If a closure unintentionally retains a reference to a large object after that object is no longer needed, which problem may occur in memory management?

    1. A memory leak may happen because the object cannot be garbage collected.
    2. The closure makes the reference thread-safe, preventing leaks.
    3. The object only exists in local scope and cannot persist.
    4. The large object will always be recycled right away.

    Explanation: Closures can retain references to variables in their outer scope, which may prevent those objects from being garbage collected when they are no longer needed, leading to memory leaks. Option B is wrong because references prevent immediate recycling. Option C is incorrect, as thread-safety does not impact memory leaks caused by retained references. Option D misunderstands the effect of closures on local variables.

  3. Event Listener Mismanagement

    Which of the following scenarios can result in a memory leak due to improper event listener scoping in an interactive application?

    1. Attaching an event listener to a DOM element and forgetting to remove it after the element is removed.
    2. Using a try-catch block for error handling around event logic.
    3. Declaring event handler variables with proper block scope.
    4. Assigning a variable in a local function and deleting it after use.

    Explanation: If an event listener remains active after the related element is removed, it retains references to the element and associated objects, causing memory leaks. Option B describes good memory management and does not cause leaks. Option C is unrelated to scoping or leaking. Option D suggests correct scoping for handlers, which prevents leaks rather than causing them.

  4. Improper Use of Global Arrays

    What can occur if a continually growing global array is used to store temporary data but never cleared or limited in size?

    1. The array's memory usage is limited by default to one hundred items.
    2. The array will automatically shrink when memory is needed.
    3. The application may experience a memory leak as the array grows without bounds.
    4. The data is garbage collected after each array push operation.

    Explanation: Failing to clear or limit the size of a global array allows it to accumulate data, leading to increased memory usage and potential leaks. Option B is incorrect; arrays do not shrink automatically. Option C falsely assumes a default size limit, which generally does not exist. Option D is wrong because pushed data remains until manually removed or the array is cleared.

  5. Scoping with Timers

    If a timer callback in a program captures a reference to an object and the timer is never cleared, what consequence can this have on memory usage?

    1. The timer automatically clears itself after one loop.
    2. The callback does not affect object lifespan, so there is no risk of a leak.
    3. The object may never be garbage collected, resulting in a memory leak.
    4. Objects referenced by timers are always tracked and removed safely.

    Explanation: Persistent timer callbacks that capture object references can prevent garbage collection, thus leaking memory if not cleared. Option B is inaccurate because timers often must be cleared manually. Option C incorrectly assumes that callbacks do not influence object life. Option D is inaccurate since not all timer implementations handle references automatically or safely.