简体   繁体   中英

How to reduce the memory usage of a C# console application project?

This is program consumes 36.50 MB of memory but I want it to be less than 32 MB

public static void CreateText(string text)
{
    if (Convert.ToInt32(text.Length) <= 80)
    {
        int n;
        string str = "";
        string count = "";
        char[] mas = text.ToCharArray();

        for (int i = 0; i < Convert.ToInt32(mas.Length); i++)
        {
            if (int.TryParse(mas[i].ToString(), out n))
            {
                count += mas[i].ToString();
            }
            else
            {
                if (String.IsNullOrEmpty(count))
                {
                    str += mas[i].ToString();
                }
                else
                {
                    for (int j = 0; j < Convert.ToInt32(count); j++)
                    {
                        str += mas[i].ToString();
                    }
                    count = "";
                }
            }

        }
        Console.WriteLine(str);
    } else {
        Console.WriteLine("Error");
    }
}

To reduce memory footprint you need to get read of temporary string objects generated by applying operation += against a string. String is immutable object in C#, so += creates new string. StringBuilder is mutable, so use it instead of string. You also need to have count as an int, not string or StringBuilder.

public static void CreateText(string mas)
{
    if (mas.Length <= 80)
    {
        StringBuilder str;
        int count;
        for (int i = 0; i < mas.Length; i++)
        {
            if (mas[i] >= '0' && mas[i] <= '9')
                count = count * 10 + mas[i] - '0';
            else
            {
                if (count == 0)
                    str.Append(mas[i]);
                else
                {
                    for (int j = 0; j < count; j++)
                          str.Append(mas[i]);
                    count = 0;
                }
            }
        }
        Console.WriteLine(str.ToString());
    }
    else
        Console.WriteLine("Error");
}

This probably isn't possible. Most of the RAM in a 36MB program is just core framework libraries. 36MB is nothing .

But I do see some potential improvements, the biggest of which are maintaining count as an integer rather than a string and using a string constructor and StringBuilder instead of appending to a string all the time:

public static void CreateText(string text)
{
    if (text != null && text.Length <= 80)
    {
        int n; int count = 0;
        StringBuilder result = new StringBuilder();
        char[] mas = text.ToCharArray();

        foreach(char c in text)
        {
            if (int.TryParse(c.ToString(), out n))
            {
                count = (count * 10) + n;
            }
            else
            {
                if (count == 0) 
                { 
                    result.Append(c);
                }
                else
                {
                    result.Append(new string(c, count));
                    count = 0;
                }
            }

        }
        Console.WriteLine(result.ToString());
    } else {
        Console.WriteLine("Error");
    }
}

There is a potential bug there if you want to be able to explicitly set 0 repetition in the input string. If that's the case, we'll need something that is slightly less efficient, but should still have a big improvement over the original:

public static void CreateText(string text)
{
    if (text != null && text.Length <= 80)
    {
        int n; int count = -1;
        StringBuilder result = new StringBuilder();
        char[] mas = text.ToCharArray();

        foreach(char c in text)
        {
            if (int.TryParse(c.ToString(), out n))
            {
                if (count == -1) count = 0;
                count = (count * 10) + n;
            }
            else
            {
                if (count == -1) 
                { 
                    result.Append(c);
                }
                else
                {
                    result.Append(new string(c, count));
                    count = -1;
                }
            }

        }
        Console.WriteLine(result.ToString());
    } else {
        Console.WriteLine("Error");
    }
}

Try this:

public static void CreateText(string text)
{
    if (text.Length <= 80)
    {
        var str = new StringBuilder();
        var count = new StringBuilder();

        for (int i = 0; i < text.Length; i++)
        {
            int n;
            if (int.TryParse(text[i].ToString(), out n))
            {
                count.Append(text[i]);
            }
            else
            {
                if (String.IsNullOrEmpty(count.ToString()))
                {
                    str.Append(text[i]);
                }
                else
                {
                    for (int j = 0; j < Convert.ToInt32(count.ToString()); j++)
                    {
                        str.Append(text[i].ToString());
                    }
                    count.Clear();
                }
            }
        }
        Console.WriteLine(str);
    }
    else
    {
        Console.WriteLine("Error");
    }
}

What I did was:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM