0
0
Compiler Designknowledge~30 mins

Garbage collection basics in Compiler Design - Mini Project: Build & Apply

Choose your learning style9 modes available
Garbage Collection Basics Database
📖 Scenario: You are building a simple database to track objects in memory for a garbage collector in a compiler design project. This database will help you understand how objects are allocated, referenced, and identified as garbage.
🎯 Goal: Create a database that stores objects with their IDs and reference counts, then identify which objects are eligible for garbage collection (those with zero references).
📋 What You'll Learn
Create a dictionary called objects with object IDs as keys and their reference counts as values.
Create a variable called threshold set to 0 to identify garbage objects.
Use a dictionary comprehension to create a new dictionary called garbage_objects containing only objects with reference counts equal to the threshold.
Add a final step to count the number of garbage objects and store it in a variable called garbage_count.
💡 Why This Matters
🌍 Real World
This project models how a garbage collector tracks and identifies unused objects in memory to free up space automatically.
💼 Career
Understanding garbage collection is important for compiler design, programming language development, and optimizing software performance.
Progress0 / 4 steps
1
Create the objects dictionary
Create a dictionary called objects with these exact entries: "obj1": 3, "obj2": 0, "obj3": 5, "obj4": 0, and "obj5": 2.
Compiler Design
Need a hint?

Use curly braces {} to create a dictionary with keys and values separated by colons.

2
Set the garbage threshold
Create a variable called threshold and set it to 0 to represent the reference count that identifies garbage objects.
Compiler Design
Need a hint?

Just assign the number zero to the variable threshold.

3
Identify garbage objects
Use a dictionary comprehension to create a new dictionary called garbage_objects that contains only the entries from objects where the reference count equals the threshold.
Compiler Design
Need a hint?

Use {key: value for key, value in dictionary.items() if condition} syntax for dictionary comprehension.

4
Count garbage objects
Create a variable called garbage_count and set it to the number of entries in the garbage_objects dictionary.
Compiler Design
Need a hint?

Use the len() function to count dictionary entries.