|
-
March 14th, 2009, 12:56 PM
#1
How to prevent outofmemory exeption?
I have an app that has to load large amounts of data in chunks (maybe like, half a MB in a string) but for some reason I'll get out of memory errors even though there is TONS of ram left + page file. The memory used is then cleared once the string has been processed.
Is there a way to make the program less sensible to large amounts of ram being used? If the ram is there to use, then it should use it instead of crapping out.
-
March 14th, 2009, 01:17 PM
#2
Re: How to prevent outofmemory exeption?
OutOfMemory exception usually occurs when you are trying to save something whereas the variable that you are trying to save it in does not have enough memory. It does not mean that if you declare a string object, if there is 1 GB memory free on your system, it will not use 1 GB, it will only use the specified size.
I would suggest using stringbuilder instead of a string if your data is too large.
-
March 14th, 2009, 01:18 PM
#3
Re: How to prevent outofmemory exeption?
 Originally Posted by Red Squirrel
I have an app that has to load large amounts of data in chunks (maybe like, half a MB in a string) but for some reason I'll get out of memory errors even though there is TONS of ram left + page file. The memory used is then cleared once the string has been processed.
Is there a way to make the program less sensible to large amounts of ram being used? If the ram is there to use, then it should use it instead of crapping out.
Remember objects over 80,000 bytes (that is 40,000 chars in Unicode) get allocated on the LArgeObject Heap, which is sucbject to fragmentation.
Use your debugger tools to look at how the memory is being used at the time of the OOM.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 02:01 PM
#4
Re: How to prevent outofmemory exeption?
How would I go about ensuring it does not get fragmented?
I've also read I should try to reuse objects instead of redeclaring, will that help? Right now the string that holds all the data gets redeclared for each object (over 800k) some are small while some are very large.
-
March 14th, 2009, 02:06 PM
#5
Re: How to prevent outofmemory exeption?
 Originally Posted by Red Squirrel
How would I go about ensuring it does not get fragmented?
Dont allocate and release blocks of differig sizes that are over 80,000 bytes in size. 
[qupte]
I've also read I should try to reuse objects instead of redeclaring, will that help?
[/quote]
Re-Read above...
[/quote]Right now the string that holds all the data gets redeclared for each object (over 800k) some are small while some are very large.[/QUOTE]
Strings are immutable, and ALWAYs involve reallocation.
If you combine these, irt means one very simple thing...A robust application should not have strings over 40,000 (actually slightly smaller). PERIOD.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 02:20 PM
#6
Re: How to prevent outofmemory exeption?
 Originally Posted by Red Squirrel
I have an app that has to load large amounts of data in chunks (maybe like, half a MB in a string)
Do you actually need all that in memory at the same time? If so, do you actually need it to be a single chunk of memory? As TheCPUWizard said, you're more than likely hitting issues with the large object heap. If you avoided having to have all that data in-memory in one single chunk of memory, you'd completely avoid the issue.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 14th, 2009, 03:05 PM
#7
Re: How to prevent outofmemory exeption?
 Originally Posted by TheCPUWizard
Dont allocate and release blocks of differig sizes that are over 80,000 bytes in size.
I've also read I should try to reuse objects instead of redeclaring, will that help?
Re-Read above...
Right now the string that holds all the data gets redeclared for each object (over 800k) some are small while some are very large.
Strings are immutable, and ALWAYs involve reallocation.
If you combine these, irt means one very simple thing...A robust application should not have strings over 40,000 (actually slightly smaller). PERIOD.
[/quote]
I don't have a choice since I need to load that data into memory, and it comes from a SQL database so it needs to be taken in one chunk, processed, then move to next row. Thing is, it's freed up after.
What's the point of having 3GB of ram FREE if I can't even use it? If I had the choice this app would be written in C++ which has less limitations but I don't have a choice here. There's got to be a way to get around these errors without "not using that much ram in one variable" since I'm not even using close to what there is available.
Last edited by Red Squirrel; March 14th, 2009 at 03:16 PM.
-
March 14th, 2009, 03:09 PM
#8
Re: How to prevent outofmemory exeption?
 Originally Posted by Mutant_Fruit
Do you actually need all that in memory at the same time? If so, do you actually need it to be a single chunk of memory? As TheCPUWizard said, you're more than likely hitting issues with the large object heap. If you avoided having to have all that data in-memory in one single chunk of memory, you'd completely avoid the issue.
Yes these strings have to be in single chunks. The way the app was engineered at first was not very SQL friendly, but I could not keep it using files as it was getting inefficient. So now each Item, Mobile, etc is stored as a string of data into a SQL row. This row is then read and put in a string, this string is then processed and the individual variables of the object are populated, then the string is discarded. This process only happens when the world has to load.
The program is for a game server so we're talking TONS of data here, of various lengths. I will be optimizing things as I go. The biggest culpit now is house data so I may find another way to store these but I can't see how this method can't work considering I have enough ram anyway.
Eventually I may convert this app to C++ to overcome .net / C# limitations, but that's a long way ahead. I prefer dealing with pointers and stuff as at least I have full control over memory management.
-
March 14th, 2009, 03:20 PM
#9
Re: How to prevent outofmemory exeption?
I sincerely doubt that you will be able to get a stable solution without a complete re-design. This one symptom alone indicates that the developer(s) had no clue as to how to write a proper .NET application.
Translation....
If I was approached to take this on as a paid contract, the ONLY way I would approach the job is as a complete re-design and re-write. REPEATED experience has shown that unless an application is designed from the ground up following best practices, that attempting to retro-fit it, is unlikely to succeed and will almost always result in major cost over runs, and a low probability of success...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 05:26 PM
#10
Re: How to prevent outofmemory exeption?
 Originally Posted by TheCPUWizard
I sincerely doubt that you will be able to get a stable solution without a complete re-design. This one symptom alone indicates that the developer(s) had no clue as to how to write a proper .NET application.
Translation....
If I was approached to take this on as a paid contract, the ONLY way I would approach the job is as a complete re-design and re-write. REPEATED experience has shown that unless an application is designed from the ground up following best practices, that attempting to retro-fit it, is unlikely to succeed and will almost always result in major cost over runs, and a low probability of success...
I agree with that, and that is actually plan B, to redo the whole thing from scratch, but it will take a very long time, and I wish to end this project by 2010. So I'm hoping there's a way around this issue such as a function of some sort to make it defragment the memory (probably very often) durring the load process.
An idea I also had was to have a C++ app read the DB and dump the contents to file, then have the program read from the file this way I can control my buffer size, but this will add more complexity as well.
I was testing and I can hold a few GB of data in a single string. I had a loop that just kept concatenating a char.
-
March 14th, 2009, 05:31 PM
#11
Re: How to prevent outofmemory exeption?
I'll bet you that if you take the "lets fix it incrementally" approach that you will NOT have a robust working product by the end of 2010.....
There is NO way to defragment the LOH. EVERY application developer should be using a performance monitoring tool while they are writing and testing code and be measuring the number of LOH allocations, and their size distribution, ESPECIALLY if they have a program that they intend to be continously executing (ie a "sever" program).
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 08:32 PM
#12
Re: How to prevent outofmemory exeption?
Hmm so guess I'll have to have a secondary app that reads the data from the DB then outputs to file then. This way I can read it in smaller buffers. Thing is, it's not like I'm keeping those strings filled, it's just a buffer that is temp storage for a SQL row and then formatted properly in the program. a proper DB structure is not an option for this app unless I recode it from scratch which I'm trying to avoid.
Yes if I do it from scratch it would be 10 times more efficient, but I just don't want to spend that much time on it.
I've asked myself many times if I should though as I keep running into road blocks and it's usually a limitation of C# or just something that is horribly coded.
-
March 15th, 2009, 06:21 AM
#13
Re: How to prevent outofmemory exeption?
 Originally Posted by Red Squirrel
I've asked myself many times if I should though as I keep running into road blocks and it's usually a limitation of C# or just something that is horribly coded.
I have yet to find a SINGLE instance of the former (limitation of the language), but have professionally encountered thousands of the latter (horrible code).
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 15th, 2009, 08:02 AM
#14
Re: How to prevent outofmemory exeption?
The program is for a game server so we're talking TONS of data here, of various lengths..
Eventually I may convert this app to C++ to overcome .net / C# limitations, but that's a long way ahead. I prefer dealing with pointers and stuff as at least I have full control over memory management.
C# should not be a limitation when writing a game server. There are some massive games out there using C# which have no scalability issues. SecondLife would be the biggest one that I can think of right now. Bear in mind that if decide to rewrite in C++, you'll be starting from scratch. Maybe it'd be better to just start from scratch and rewrite it properly in C#.
I've asked myself many times if I should though as I keep running into road blocks and it's usually a limitation of C# or just something that is horribly coded.
If performance is a concern and the current architecture does not allow for performance to scale, then the current architecture design needs to revisited and altered. If that requires a rewrite, then so be it. It's better to do it now rather than 3 months before the release. This is a question you need to answer asap.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 17th, 2009, 01:46 PM
#15
Re: How to prevent outofmemory exeption?
 Originally Posted by Red Squirrel
This row is then read and put in a string
Does the whole row need to be read in one go?
Is there no way to read part of a row?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|