© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
Animal

❔ Out of memory exception

Hello,

I have the following code

private static void StringBuilderTest()
    {
        ConcurrentDictionary<int, string> concurrentDictionary = new ConcurrentDictionary<int, string>();
        for (int i = 0; i < 1500; i++)
        {
            concurrentDictionary[i] = getBigStringWith60KCharecters();
        }

        int counter = 1;
        Collection<int> ids = new Collection<int>();
        StringBuilder caseExpression = new StringBuilder();
        try
        {
            foreach (KeyValuePair<int, string> keyValuePair in concurrentDictionary)
            {
                ids.Add(keyValuePair.Key);
                caseExpression.AppendLine($"WHEN Document_Id = {keyValuePair.Key} THEN {keyValuePair.Value}");
                const int MAX_PREFERD_AMOUNT_OF_CHARECTERS = 100000000;
                if (caseExpression.Length > MAX_PREFERD_AMOUNT_OF_CHARECTERS || counter == concurrentDictionary.Count)
                {
                    string tempString = caseExpression.ToString();
                    TryUpdateBatchOfWebDocuments(tempString, ids);
                    caseExpression.Clear();
                    ids.Clear();
                }

                counter++;
            }
        }
        catch (Exception exc)
        {
            throw;
        }
    }

private static void StringBuilderTest()
    {
        ConcurrentDictionary<int, string> concurrentDictionary = new ConcurrentDictionary<int, string>();
        for (int i = 0; i < 1500; i++)
        {
            concurrentDictionary[i] = getBigStringWith60KCharecters();
        }

        int counter = 1;
        Collection<int> ids = new Collection<int>();
        StringBuilder caseExpression = new StringBuilder();
        try
        {
            foreach (KeyValuePair<int, string> keyValuePair in concurrentDictionary)
            {
                ids.Add(keyValuePair.Key);
                caseExpression.AppendLine($"WHEN Document_Id = {keyValuePair.Key} THEN {keyValuePair.Value}");
                const int MAX_PREFERD_AMOUNT_OF_CHARECTERS = 100000000;
                if (caseExpression.Length > MAX_PREFERD_AMOUNT_OF_CHARECTERS || counter == concurrentDictionary.Count)
                {
                    string tempString = caseExpression.ToString();
                    TryUpdateBatchOfWebDocuments(tempString, ids);
                    caseExpression.Clear();
                    ids.Clear();
                }

                counter++;
            }
        }
        catch (Exception exc)
        {
            throw;
        }
    }


The code in question involves the manipulation of strings using both the string and StringBuilder classes. From my understanding, neither the string nor the StringBuilder object is reaching its maximum capacity, which is approximately 2 billion characters. Despite this, I am still encountering the memory exception.

I am wondering if there could be any other factors contributing to this issue. Are there any limitations on the maximum capacity of virtual memory that I should be aware of?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Out of Memory Exception Help
C#CC# / help
3y ago
Getting Out of Memory Exception when loading in images
C#CC# / help
2y ago
Out of memory error
C#CC# / help
2y ago
❔ Index Out of Bounds Exception
C#CC# / help
3y ago