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.
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?
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.
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?
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.
Which of the following scenarios can result in a memory leak due to improper event listener scoping in an interactive application?
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.
What can occur if a continually growing global array is used to store temporary data but never cleared or limited in size?
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.
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?
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.