Library tutorials & articles

Memory in .NET - what goes where

Introduction

A lot of confusion has been wrought by people explaining the difference between value types and reference types as "value types go on the stack, reference types go on the heap". This is simply untrue (as stated) and this article attempts to clarify matters somewhat.

What's in a variable?

The key to understanding the way memory works in .NET is to understand what a variable is, and what its value is. At the most basic level, a variable is just an association between a name (used in the program's source code) and a slot of memory. A variable has a value, which is the contents of the memory slot it's associated with. The size of that slot, and the interpretation of the value, depends on the type of the variable - and this is where the difference between value types and reference types comes in.

The value of a reference type variable is always either a reference or null . If it's a reference, it must be a reference to an object which is compatible with the type of the variable. For instance, a variable declared as Stream s will always have a value which is either null or a reference to an instance of the Stream class. (Note that an instance of a subclass of Stream , eg FileStream , is also an instance of Stream .) The slot of memory associated with the variable is just the size of a reference, however big the actual object it refers to might be. (On the 32-bit version of .NET, for instance, a reference type variable's slot is always just 4 bytes.)

The value of a value type is always the data for an instance of the type itself. For instance, suppose we have a struct declared as:

struct PairOfInts
{
    public int a;
    public int b;
}

The value of a variable declared as PairOfInts pair is the pair of integers itself, not a reference to a pair of integers. The slot of memory is large enough to contain both integers (so it must be 8 bytes). Note that a value type variable can never have a value of null - it wouldn't make any sense, as null is a reference type concept, meaning "the value of this reference type variable isn't a reference to any object at all".

Comments

  1. 05 Jul 2005 at 10:32

    Whilst this posting is accurate, it does not go a whole long way to explaining the value of value types.  


    Consider the scenario of a large scale banking system, where system testing suddenly flips a very large number into floating point format (e.g. 1.6e20), and the Java developers say it is major change with performance implications to fix.. a daft statement from a business perspective...


    why is it a big changes: because Java does not have a value-type for big decimal numbers (BigDecimal is an object reference type).. and people commonly use double for currency because of the huge overhead (performance and coding) of using java.math.BigDecimal.


    struct exists in C# because there are a small number of scenarios where the language would not be appropriate/practical without them {Point (x,y), Complex, Decimal} are common examples of why value types that are essential.. so is there a down-side yes being value-types, they are passed by value (unless the “ref” parameter modifier is used) on the stack.. a Complex number is a good example, a vector of  3000 doubles is a poor example because it is copied every time (unless ref is used).


    A cool thing about C# is that you can decide later whether an object needs to be a value-type or not, but judicious use can make an application really fly because values can live of the stack

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Memory in .NET - what goes where.

Leave a comment

Sign in or Join us (it's free).

Jon Skeet C# MVP currently living in Reading and working for Google.
AddThis

Related discussion

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

Want to stay in touch with what's going on? Follow us on twitter!