Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi

iam getting the error , when i am trying to deal with the data 20,000 records, how to solve this please help me , the errors are


The runtime has encountered a fatal error. The address of the error was at 0x58e29ddd, on thread 0x10c8. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.


Exception of type 'System.OutOfMemoryException' was thrown.

try
            {
                int k = 0;
                mobiletxt.Text = "";
                foreach (DataRowView  item in checkedListView.SelectedItems)
                {
                    string s = item.Row.ItemArray[1].ToString();
                    if (k == 0)
                    {
                        if (s.ToString() != "")
                        {
                            mobiletxt.Text = s.ToString();
                        }
                    }
                    else
                    {
                        if (s.ToString() != "")
                        {
                            mobiletxt.Text = mobiletxt.Text + "," + s.ToString();
                        }
                    }
                    k = k + 1;
                }
            }
            catch (Exception) { }

this code runs for 20000 time because it was in the foreach na,
these are the two errors that i am getting, 95% it was comming and other 5% it was not comming, what it means please help me out.
thanks in advance
Posted
Updated 28-Sep-11 7:52am
v2

Hi
CLR will allow having object size not more than 2147483647 bytes total, But string will take only by half of this size i.e 1073741823 characters and this is because of each character in string eats up 2 bytes..

So, I guess no control shows this much data in single control by means of using string datatype

There are few issues with your code, i changed the code...so try with this

C#
mobiletxt.Text = "";
                var listValues = new List<string>();
                foreach (DataRowView item in checkedListView.SelectedItems)
                {
                    string temp = item.Row.ItemArray[1].ToString();
                    if (!string.IsNullOrEmpty(temp))
                    {
                        listValues.Add(temp);
                    }
                }
                mobiletxt.Text = string.Join(",", listValues.ToArray());
</string>
 
Share this answer
 
Error is here:
C#
mobiletxt.Text = mobiletxt.Text + "," + s.ToString();

mobiletxt.Text
can't hold more data.
Remove this
C#
mobiletxt.Text = mobiletxt.Text + "," + s.ToString();
line run again and there will not be that same error.
 
Share this answer
 
Comments
Simon Bang Terkildsen 28-Sep-11 15:46pm    
you got to be kidding me right? You state it like you expect a null reference exception, which it does not.
And seeing that the entire purpose of the loop is to concatenate the string it's necesarry the way the OP does it
Аslam Iqbal 28-Sep-11 15:57pm    
You missed a bit about this: mobiletxt.Text = mobiletxt.Text + "," + s.ToString();
May be my answer let you to update your answers last version.
Simon Bang Terkildsen 28-Sep-11 17:02pm    
Do not alter my answer! thank you
You are doing into memory what you should be doing to disc. Meet File.AppendAllText[^].
 
Share this answer
 
Use less memory

Not helpful? Well then provide more details, like what the hell you're actually doing, like the code that causes the exception.


There is no problem in the posted code, but the fact that you you display 20.000+ is in my opinion, a user cannot get an overview of more than 200 items. And your case expects the user to select 20.000 elements you might as well have select 20.000 random elements button, no way the user has looked over all those selected elements.

The reason you do not get the exception all the time, is probably due to the GC sometimes does and does not collect enough memory from your application. And other programs that runs on your PC.

You have to accept that memory is limited resource. I don't know what your application is really all about but I suggest you rethink your UI you have way to much data loaded into your application. No application need to display that much data, hell no user can wrap their head around that much data.



Also use StringBuilder instead of concatenating the string like you do right now you're creating a up to two new strings * 20.000.

mobiletxt.Text + "," + s.ToString(); creates two new strings and seeing that mobiletxt.Text increases in size the memory allocated for those two strings grow larger and larger
 
Share this answer
 
v5
Comments
[no name] 28-Sep-11 13:53pm    
hi thanks for ur fast replay
i had updated the question see once
i had written the sample code
this is only the block that i am getting error
Simon Bang Terkildsen 28-Sep-11 14:31pm    
Answer updated, in short you need to rethink what you're doing, memory is a limited resource we have to live with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900