c# - 大字符串列表应该是不可能的

标签 c# string list compression

我已在存储字符串列表的 C# 控制台应用程序中运行代码。这段代码的目标是在 1 秒内创建尽可能大的字符串“q”集合。只是锻炼一下我的编程能力,没有实际应用。

当我运行这段代码时,它会在 1 秒内自行停止,当我计算所有字符串中的所有“q”时,我得到 214,870,505,313,584,相当于数百万亿。字符“q”占用一个字节,如果这个东西有 200 万亿字节,则意味着字符串列表超过 2 TB。

这怎么可能,是否正在进行某种自动压缩?有办法关掉它吗?

如果需要,这里是代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleAppWebScrape
{
    class Program
    {
        //the origional string
        static string l = "q";
        static DateTime d;
        static List<string> output = new List<string>();

        static void Main(string[] args)
        {
            //hit enter to start
            Console.Read();
            d = DateTime.Now;

            //start the string doubling thread
            Thread thred2 = new Thread(new ThreadStart(doubleL));
            thred2.Start();
            //start the write to list thread                                                                                               
            Thread thred1 = new Thread(new ThreadStart(writeToFile));
            thred1.Start();

            while (haveTime())
            {
               //pause current thread for the remander of the second. 
            }

            long lo = howmany();

            Console.WriteLine(lo);
            Console.ReadLine();
        }

        //determines the amount of "q"s in the list of strings
        static long howmany()
        {
            long lo = 0;
            foreach (string s in output)
            {
                lo += s.Length;
            }
            return lo;
        }
        //writes to the list of strings.
        static void writeToFile()
        {
            while (haveTime())
            {
                output.Add(l);
            }
        }
        //builds a string by doubling the origional string
        static bool tobig = false;
        static void doubleL()
        {
            while (haveTime() && !tobig)
            {
                if (l.Length < 268435456)
                {
                    l = l + l;
                }
                else
                {
                    tobig = true;
                }
            }
        }
        //bool to determin if it is running inside one second
        static bool haveTime()
        {
            if ((DateTime.Now - d).TotalSeconds < 1)
            {
                return true;
            }
            return false;
        }
    }
}

最佳答案

我不久前读到字符串存储在堆上并被引用。由于它一遍又一遍地使用相同的字符,因此您可能会创建数万亿个对同一物理地址的引用。

关于c# - 大字符串列表应该是不可能的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710950/

相关文章:

python - 从列表中获取特定长度的字符串

java - 如何在 ArrayList 中保留唯一值的原始顺序?

c# - onchange = "Form.submit() 仅适用于我的下拉菜单中的第一个子下拉菜单

list - 如何在 Terraform 0.12 中通过列表(对象)进行 for_each

c# - 将 Blazor 项目与库项目引用发布到 Azure

java - 将特定的 int 数组转换为 String

java - 如果分隔字符串只有一项,是否有 "cleaner"方法来解析它?

javascript - 当一段文本包含引号时,如何将其转换为字符串?

c# - 值不能为空。参数名称 : request

c# - 使增量同时完成