0% found this document useful (0 votes)
20 views13 pages

Memory Management: The Stack & The Heap

Value types are stored on the stack while reference types are stored on the heap, with pointers to reference types stored on the stack. The garbage collector manages memory allocation and deallocation on the managed heap. Memory for unmanaged resources like sockets is allocated separately and must be manually freed using IDisposable. When no references to an object remain, the garbage collector frees its memory during generations of collection passes to prevent fragmentation. Serialization prepares objects for transmission by converting them to binary or text formats, while deserialization reconstructs the objects from the transmitted representation.

Uploaded by

Mihaela Maxim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Memory Management: The Stack & The Heap

Value types are stored on the stack while reference types are stored on the heap, with pointers to reference types stored on the stack. The garbage collector manages memory allocation and deallocation on the managed heap. Memory for unmanaged resources like sockets is allocated separately and must be manually freed using IDisposable. When no references to an object remain, the garbage collector frees its memory during generations of collection passes to prevent fragmentation. Serialization prepares objects for transmission by converting them to binary or text formats, while deserialization reconstructs the objects from the transmitted representation.

Uploaded by

Mihaela Maxim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Memory Management

The Stack & The Heap


Value types and reference types
Value types are placed on the Stack (and quite many times on the Heap)

Reference types are placed on the Heap (and on the stack we have a pointer to
them)
Value types
Enums

DateTime

Int, Double, Float, Short, Long

Bool

Char

All structures created by our application


Structs
is suitable for representing lightweight objects such

Example
Reference types
String

Object

All classes created by our application


Mutable and Immutable objects
Mutable vs Immutable objects

String is immutable

StringBuilder is mutable

Example how to create a string (class Author)


Application Memory
When our program (process) starts a memory zone gets allocated by the
operating system - managed heap (yes there is an unmanaged one)

Memory gets allocated and deallocated to our process all the time

All objects get allocated on this managed heap, but they can have some memory
allocated on the unmanaged heap
Managed vs Un-Managed Memory
Garbage Collector is the part of .NET that allocates and deallocates managed
memory

The unmanaged memory: sockets (for http calls, database access etc), graphics
buffers, streams. This is unmanaged because we need to manage it ourselves:
IDisposable.Dispose()
Memory allocation
Function calls are placed on stack

Value type parameters are copied on the stack

Reference type parameters have pointers (references) on the stack

Allocation for references is easy, they get placed in order


Memory deallocation
Simple: When a reference type doesn’t have any more references to it, then it is
collected by the Garbage Collector

Problem is (like in hard disks) fragmentation

The Garbage Collector creates a graph of objects starting from roots: static
objects, global objects, local variables

Then the memory gets released, while what remains gets promoted

At the end data gets compacted


Garbage collector - generations
Generations 0, 1, 2

Full garbage collection = generation 2 collection (collections are inclusive)

Big objects (>85k) go directly to LOH, usually LOH is not compacted

OutOfMemoryException
Ref & Out
Ref: the value passed is already set, so in the method we can use it and modify it
(the whole instance)

Out: the value passed is not set, so in the method we must set it (and not use it)
Serialization / Deserialization
Serialization: preparing the object so we can send it over the wire (network)

● Binary serialization
● Text serialization: xml, json, google protobuf

Deserialization: transforming the text/binary format back into an object

You might also like