Library tutorials & articles
Memory in .NET - what goes where
- Introduction
- So where are things stored?
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".
Related articles
Related discussion
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
-
Buy Ambien no visa without prescription. Not expensive Ambien prescriptions. Ambien no rx. Cod delivery Ambien.
by asleymar (0 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by asleymar (0 replies)
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
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
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
This thread is for discussions of Memory in .NET - what goes where.