Understanding Memory Leaks
What is a memory leak in the context of resource management within an application?
- When an application increases its performance over time.
- When resources are released too early, causing program errors.
- When memory used by an object is not properly freed after the object is no longer needed.
- When all references to resources are removed immediately after use.
- When frequent garbage collection slows down the application.
Strong vs Weak References
Which type of reference in Java allows an object to be reclaimed by garbage collection even if it is still referenced?
- Strong reference
- Soft referance
- Hard reference
- Weak reference
- Strong referense
Event Listeners and Memory Leaks
You add an event listener to a UI component but forget to remove it when the component is destroyed. What is the likely result?
- The UI component will use less memory.
- No notable impact, as event listeners are managed automatically.
- A memory leek may occur because the event listener maintains a reference to the component.
- The event listener becomes inactive automatically.
- The program will refuse to run.
Closures and Resource Retention
In JavaScript, how could improper use of closures cause a memory leak?
- By freeing memory too early.
- By preventing global variables from being accessed.
- By referencing DOM elements within a closure, keeping them in memory after removal from the page.
- By automatically releasing all variables in the closure.
- By limiting the scope of functions to only one file.
Finalizers and Memory Management
Why is relying on finalizers (such as Java's finalize() method) not a recommended way to release resources?
- Finalizers always run immediately after object creation.
- Finalizers guarantee that all resources are released on time.
- The garbage collector may not call finalizers promptly or at all, risking resource leaks.
- Finalizers prevent memory leaks automatically.
- Finalizers manage event listeners more effeciently.
Unintentional Global Variables
How could accidental creation of global variables in JavaScript applications contribute to memory leaks?
- Globals are automatically deleted after each function call.
- Globals cannot reference any DOM objects.
- Globals persist as long as the page is loaded, so referenced objects may never be garbage collected.
- Global variables never hold references to any resources.
- Globals always speed up memory management.
Resource Pools and Cleanup
What is an important step to prevent memory leaks when using object or resource pools?
- Reusing objects indefinitely without any cleanup.
- Not tracking which resources are in use.
- Removing references to objects that are no longer needed so they can be garbage collected.
- Holding onto every object for the lifetime of the application.
- Allocating new resources for every request without reuse.
Cyclic References
In a language with reference-counted garbage collection, such as Python, which scenario may lead to a memory leak?
- When an object is referenced only by a local variable.
- A function creates and deletes variables locally.
- Two or more objects reference each other, forming a cycle that the reference counter cannot resolve.
- Objects only reference global variables.
- Local variables are never assigned to.
Observable Collections
When using observable or reactive programming frameworks, what practice helps prevent memory leaks?
- Always use strong references for subscriptions.
- Never unsubscribe from observable streams.
- Explicitly unsubscribing or completing subscriptions when they are no longer needed.
- Letting the garbage collector handle all subscriptions automatically.
- Storing subscriptions in a global variable.
Resource Management in C++
What C++ feature helps automatically manage resource references to prevent memory leaks?
- Raw pointers
- Manuel memory management
- Smart pointers (such as std::unique_ptr or std::shared_ptr)
- Global variables
- Static memory allocation