[英]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这是程序消耗 36.50 MB 的内存,但我希望它小于 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. String 在 C# 中是不可变对象,因此
+=
创建新字符串。 StringBuilder is mutable, so use it instead of string. StringBuilder 是可变的,所以用它代替字符串。 You also need to have count as an int, not string or StringBuilder.
您还需要将 count 作为 int,而不是 string 或 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 程序中的大部分 RAM 只是核心框架库。 36MB is nothing .
36MB 不算什么。
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:但我确实看到了一些潜在的改进,其中最大的改进是将
count
保持为整数而不是字符串,并使用字符串构造函数和 StringBuilder 而不是一直附加到字符串:
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.如果您希望能够在输入字符串中显式设置 0 重复,则存在一个潜在的错误。 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:我所做的是:
char[]
since your can treat a String
as one (it implements the indexer)char[]
因为您可以将String
视为一个(它实现了索引器)n
closed to used to it's only allocated if neededn
关闭以适应它仅在需要时分配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.