CPU & Memory Performance Flashcards
(64 cards)
What is something that is stored automatically on the heap and not the stack?
Anything with the “new” keyword in C#
What is the stack?
It is the region of memory used for storing local variables, method calls, and function parameters.
It works in a Last In, First Out (LIFO) way—meaning the last item pushed on is the first one popped off.
It is used automatically by the runtime when methods are called and exited. For example, each time a method is called, a new “stack frame” is created for its variables.
Stack memory is fast but limited in size, and it’s cleared automatically when a method exits.
The role of the garbage collector is to …
Deallocate dereferenced objects on the heap
What is the heap?
It is the region of memory used for storing objects and data that need to live beyond the scope of a method call.
Unlike the stack, the heap is not automatically cleared—instead, the Garbage Collector (GC) cleans it up when objects are no longer used.
Data on the heap is accessed via references (pointers), and allocating memory here is slower than on the stack, but it allows for longer-lived and larger objects.
What are the main ways memory is added to the heap?
Memory is added to the heap mainly when you use the new keyword to create objects, arrays, or strings.
Reference types (like classes, arrays, and strings) are always stored on the heap.
Value types (like int, bool, struct) go on the heap only when they’re part of a reference type (e.g., inside a class or boxed).
Which of the following .NET types is NOT a value type?
A) int
B) bool
C) string
D) double
A: ✅ C) string
string is a reference type in .NET, even though it behaves somewhat like a value type because it’s immutable.
All the other options (int, bool, double) are value types, which are stored directly on the stack (unless boxed).
What is the difference between a value and reference type?
Value Type
- Stores the actual data directly.
- Typically stored on the stack.
- Copied when assigned to a new variable or passed to a method.
Examples: int, bool, double, struct.
Reference Type
- Stores a reference (pointer) to the data, not the data itself.
- Data is stored on the heap.
- Multiple variables can point to the same object in memory.
Examples: string, class, array, object.
Key Difference:
Assigning a value type creates a copy, but assigning a reference type creates a shared reference.
A reference type variable does not contain an object, but instead only holds a reference to an object where?
On the heap.
Two reference type variables can refer to the same object on the heap.
True or False
True
Reference types are assigned by reference. What does this mean?
When a reference type is assigned to another variable, both variables point to the same object in memory (the reference is copied).
This means changes made through one variable will affect the same object seen by the other.
class Person { public string Name; }
var a = new Person { Name = “Alice” };
var b = a;
b.Name = “Bob”;
Console.WriteLine(a.Name);
// Output: “Bob”
How much memory does any reference take?
A reference (the pointer to an object on the heap) typically takes:
4 bytes on a 32-bit system
8 bytes on a 64-bit system
This is because a reference is just an address pointing to the object’s location in memory, and its size depends on the architecture of the system (32-bit vs 64-bit).
class Person { public string Name; }
// Why are these not considered equal?
var a = new Person { Name = “Alice” };
var b = new Person { Name = “Alice” };
Console.WriteLine(a == b); // False
Because a and b are reference types, the == operator checks if they refer to the same object in memory, not whether their contents are equal.
Boxing happens when …..
a) A variable of type ‘object’ is cast to a reference type.
b) A variable of type ‘object’ is cast to a value type.
c) Storing a reference type in a variable of type ‘object’.
d) Storing a value type in a variable of type ‘object’.
d) Storing a value type in a variable of type ‘object’.
Unboxing happens when…
a variable of type ‘object’ (which holds a boxed value type) is explicitly converted back to its original value type.
class Person { public int Age; }
var a = new Person {Age = 20 };
var age = (int)a.Age;
What is the primary difference in portability between CIL (Common Intermediate Language) and Assembly Language?
CIL is platform-independent, meaning it can run on any system with the .NET Common Language Runtime (CLR). Assembly Language is platform-dependent, specific to a particular CPU architecture (e.g., x64, ARM).
What component in the .NET ecosystem is responsible for converting CIL into native machine code that the CPU can execute?
The Just-In-Time (JIT) compiler, which is part of the Common Language Runtime (CLR).
Does Assembly Language directly control hardware, or does it rely on a virtual machine?
Assembly Language directly controls hardware, using instructions that the CPU executes natively. It does not rely on a virtual machine.
When you compile C# code, what language does it first get translated into?
It first gets translated into CIL (Common Intermediate Language).
What benefit does .NET gain by compiling to CIL first, rather than directly to Assembly Language?
It gains platform independence and managed execution. CIL allows .NET applications to run on different CPU architectures without recompilation, and the CLR provides services like garbage collection and type safety.
Strings are immutable in .NET. What does this mean?
Once a string object is created, its value cannot be changed. Any operation that appears to modify a string actually creates a new string object in memory with the new value, leaving the original string unchanged.
Why is immutable strings is useful?
It allows for super-fast assignment and comparing of strings.
When is using the StringBuilder preferred?
StringBuilder is preferred when you anticipate performing a significant number of string modifications (like appending, inserting, or replacing characters/sub-strings) because string objects are immutable, leading to inefficient memory allocation and performance issues with repeated modifications. StringBuilder is mutable and more efficient for such scenarios.
For strings, every dereferenced string will be left for the GC to clean up, but if using a StringBuilder, the GC only need to clean up the one object.
With strings, what is “interning” and why is it useful?
Interning is a process where the .NET runtime stores a single, unique copy of each literal string (and optionally, other strings) in a special memory area called the “intern pool.” It’s useful because it saves memory and allows for faster string comparisons.
So if two variables are “intern” the same large paragraph, the compiler reuses the reference for both. When they are compared, only the reference is compared. On a x64 operating system, this is an 8 byte comparison.
Strings are reference types but behave like value types.
True or False
True