Question

I need to save huge number of boolean values (bits) in memory. If I using List<bool> after some time I get OutOfMemoryException, Is there any way to save big number of bits without splitting to chunks?

Was it helpful?

Solution

Code your boolean variables. I give you an example:

assume 32 bool variables are in list: 1001001100101100000001011010010

If you want to save all these variables in memory without coding them, you should use at least 10 bytes, but by using a simple coding algorithm you can save significant amount of your memory. A simple suggestion algorithm for these 32 boolean variables is to save them as a 32bit int =1234567890. In this way memory usage will slake to 4 bytes only!

It is natural than coding of every data needs processing it, so CPU usage will be increase instead of memory usage.

OTHER TIPS

You can still use List<bool>, however, if you're trying to use very large lists in 64 bit environments you need to enable large objects in the application configuration.

Get more info from : MSDN

Take a look at BitArray class, which provides a compact way to store bit masks

Have you tried simply reserving the space up-front? that way the list doesn't have to worry about growth for a good long while. You've hypothetically stated in your comment that it should be able to store 250 million. But the following runs just fine at least for me and should potentially allow you to store more since it's not having to constantly resize:

List<bool> test = new List<bool>(1000000000);

That being said, you would take up much less memory to store this at the bit level. That solution would probably be preferable but you'll have to write quite a bit more custom code for this.

Also...it seems like a really strange request. Perhaps there's a different way to approach your problem?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top