Murhaf Sousli
Murhaf Sousli

Reputation: 13296

Which takes more memory, string value or long value?

I have a client application that connects to a WCF service, I get the file size from the server as a long value then I convert it at the client to string so it appears like ex:52.21 MB

The application gets too many files sizes every time user changes the current directory path.

My question: Should I convert the values to string format from the WCF service app then return it to the client as a string or should I just return the size as a long value and let the client to convert it to a string format

In other way which value take more bytes in the memory:

long size = 55050240;
string size = "52.5 MB"; 

long large_size = 56371445760;
string large_size = "52.5 GB";

UPDATE:

I use this method to convert long value to a string format:

private string ConvertUnit(long source)
    {
        const int byteConversion = 1024;
        double bytes = Convert.ToDouble(source);

        if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
        {
            return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
        }
        else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
        {
            return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
        }
        else if (bytes >= byteConversion) //KB Range
        {
            return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
        }
        else //Bytes
        {
            return string.Concat(bytes, " Bytes");
        }
    }

Short question: Which takes more memory, string value or long value?

Upvotes: 2

Views: 8339

Answers (3)

M Afifi
M Afifi

Reputation: 4795

"Which takes more memory, string value or long value"

Strings in C# are Unicode characters (every character is two bytes) + a pointer to that address (minimum 4 bytes, 8 bytes on an x64 .NET application).

Long is 8 bytes. Once you exceed 2 characters on x86, or any characters on x64 you'll take more memory than long.

Strings are immutable though, if you're sending a large number of identically sized files, string would end up consuming less memory.

For your actual question, you should use long to truly represent the underlying object.

Edit: Corrected to take into account pointer size for string.

Upvotes: 5

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

A .NET string is an array of UTF-16 code units, each 2 bytes long. So, "52.5 MB" will take 7*2=14 bytes, even disregarding the reference to string (4 or 8 bytes depending on process "bit-ness") and auxiliary fields such as string length. The long is 8 bytes, so it is clear the string is much "fatter".

Also, your examples don't convey the same information (you are loosing "precision" in string), so it is like comparing apples and oranges.

Even encoded in SOAP messages, long should be more compact than string.

For all these reasons, I'd go with long.

Upvotes: 2

ColinE
ColinE

Reputation: 70122

Having a service layer that output values that are formatted is a very bad idea. What happens if the requirements change and rather than a GB suffix, the client application UI needs to output in bytes? e.g. 56,371,445,760 ? This will required the client application to parse then re-format the data sent by the server.

Also, determining the memory required to store an individual variable is easy. What is not so easy is to work out the overall impact to your application. Different types will be handled differently in your code, and as a result a smaller variable might incur a greater cost.

Upvotes: 4

Related Questions