TheDeveloperBlog.com


C# Decimal Memory

Decimal values require 16 bytes. The decimal type is a value type. It requires more memory than most other value types used commonly in the C# language. It stores more data as well.


Example. First, this program shows the byte size of the decimal type is equal to 16 bytes. It does this through a simulation that measures the number of bytes allocated in the managed heap before and after an array of decimal is allocated.

And: For this reason, we can be assured that the decimal type is 16 bytes in size.

C# program that measures array of decimals

using System;

class Program
{
    const int _count = 1000000; // One million decimals
    static void Main()
    {
	long bytes1 = GC.GetTotalMemory(true);
	//
	// Allocate an array of decimal values.
	// ... Assign an element to make the allocation not optimized out.
	//
	decimal[] array = new decimal[_count];
	long bytes2 = GC.GetTotalMemory(true);
	array[1] = 0.1M;
	//
	// Compute the memory usage of the decimal type.
	//
	long difference = bytes2 - bytes1;
	double per = (double)difference / _count;
	Console.WriteLine("Program used: {0:0.0} MB",
	    (double)difference / (1024 * 1024));
	Console.WriteLine("Each decimal element required: {0:0.0} bytes",
	    per);
    }
}

Output

Program used: 15.3 MB
Each decimal element required: 16.0 bytes

This program text is defined in a compilation unit and it executes the Main entry point at runtime. The GC.GetTotalMemory method is used to acquire an estimate of the number of bytes allocated in the managed heap.

And: The program allocates an array of one million decimal elements. It assigns to the array, ensuring the allocation persists.

16 bytes used. The program when executed on a 32-bit Windows operating system reports that 15.3 MB of memory is used total in the managed heap. Using division, we find that 16.0 bytes are used for each decimal element in the array.

Thus: The program proves that each decimal element occupies 16 bytes on the managed heap and the decimal is a 16-byte value type.


Sizeof. The sizeof operator in the C# language can be used on value types to return the number of bytes stored in the value type. If we run a program that evaluates sizeof(decimal), we see that it returns the integer 16.

Sizeof

Note: We show this through an experiment and prove that the value is accurate when the type is stored as an element in a large array.


Discussion. The decimal type is used in cases where high accuracy of values is required, and also large numbers are common. For this reason, currencies and monetary values are often used with the decimal type.

Because the decimal type is 16 bytes, it will not be as efficient in low-level code as the native integer. However, when the accuracy of monetary sums is at risk, reduced performance is much preferable.


Summary. We looked at the memory usage and byte size of the decimal type. Because the decimal type is from the base class library, you can also use it in VB.NET. The decimal type is useful for large and accurate value representations.

Also: The decimal type will occupy four times more memory than an integer. This can impose constraints on some systems.