C
C#2y ago
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; } } 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?
5 Replies
Jimmacle
Jimmacle2y ago
cut your assumption for maximum string capacity in half .NET objects can't be bigger than 2GB and characters are 2 bytes each
Animal
AnimalOP2y ago
well it would be still around 1 bilion im trying to allocate until around 100 milion
BlushyFace (BlushyFace.com)
... what you trying to achieve?
Animal
AnimalOP2y ago
I’m getting an document from db around 1,500 of them Doing manipulation on each one of them in parallel and after I finished I need to update the db with the new document so I create a big query to update the db
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server