Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below code To save data table data in to list I have 1 million records in datatable but after 524288 row I get this error An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code how can I resolve it?
thanks
Posted

Quote:
I have the below code
What code?

Memory - by its nature - is limited!
Before you ask yourself how to fill a list with 1 million records, ask yourself: 'what will I do with that list?'...
Present to the end user? No way any user can handle a list of 1 million items?
Save onto some other media (disk? DB?). Do it bu chunks/pages, that will save some memory for you...
 
Share this answer
 
Comments
Angela 10293848 26-Nov-14 3:10am    
if I select 100000 i get similliar error
Kornfeld Eliyahu Peter 26-Nov-14 3:16am    
In software design 100,000 is as bad as 1,000,000!!!
Angela 10293848 26-Nov-14 3:30am    
oh come on I do not think it is too large in these days
Sinisa Hajnal 26-Nov-14 3:33am    
It is not too large (depending on the type you're keeping them in) - but from design perspective, it is bad. You should only keep in memory those items you're actively working with or which you plan on showing. All others should be safely in the database.

And if you get the error for 100 000 either you have really large memory footprint or you're copying it (without realizing it) several times in the memory...in the inital question you said you get the error around 500 000 so how do you run out of it in 100 000 scenario? You're doing something. Please psot some code. Thank you.
Kornfeld Eliyahu Peter 26-Nov-14 3:35am    
It is a fact that your application/computer can not handle it! So what do you think is irrelevant!!!
You are however right that a million records is not to large (I have tables with hundred of millions) for data processing, but even a 100 records is large for UI...
So - as I told already - before you start running over your millions, ask yourself: what for?
AS the error says, you ran out of memory.

The solution is:
a) use smaller set of data (filtering)
b) save it partially, take first 100 000 to list and save, take another 100 000 etc
c) redesign so that you only keep single page of data in memory, the rest is called as needed from the depository (database)
d) increase the memory available to the process :)
 
Share this answer
 
Comments
Angela 10293848 26-Nov-14 2:51am    
I need to select 1 million
Kornfeld Eliyahu Peter 26-Nov-14 3:15am    
Why? Even you had a million dollar you would never walk around with it, but put most of it in a bank (or under the bed)...
Sinisa Hajnal 26-Nov-14 3:30am    
If you think you need them for some kind of processing - no, you don't. Databases are good at processing millions of rows of data. You're limited to your system resources. Simply selecting them for whatever reason is never good idea. You'll never show a list of million items so there is no need to keep them in memory.

If you specify your requirements, we might be able to help. The solution above offers you options. You cannot avoid physical limitations (in this case your memory capacity). Whatever you might need / wish / command is irrelevant, it will not change the physical realities.
Angela 10293848 26-Nov-14 3:33am    
I need it to provide a report yes you are right end user can not see all records together but He can filter some data that who need so the grid view should have all data
Sinisa Hajnal 26-Nov-14 3:37am    
Make them filter it before getting the data. At least in general sense (for example only current year of something)

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