Understanding Object Garbage Collection

Medium Object Lifecycle & Constructors
Solve in Playground →

Problem Statement

Create a `Resource` class that prints ‘Allocated’ in `__init__` and ‘Freed’ in `__del__`. Instantiate it and assign it to a variable `res`. Then, assign `res = None` to force garbage collection, and finally print ‘End of script’.

Input Format

None

Output Format

Print lifecycle events showing exactly when the object is freed.

Constraints

None

Sample Output

Allocated
Freed
End of script

Explanation

Reassigning the only reference to an object drops its reference count to zero, triggering __del__ immediately before the script continues.

Starter Code

class Resource:
    # Define __init__ and __del__
    pass

if __name__ == '__main__':
    res = Resource()
    # Force garbage collection
    
    print('End of script')

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used