Memory size of a list of int

核能气质少年 提交于 2019-12-06 08:04:11

An int in C# is always 4 bytes (no matter what the value). A list of 1,000 ints is therefore ~4,000 bytes. I say approximately because the list structure will add some overhead. A few thousand ints in a list shouldn't be a problem for a modern computer.

I think you are heading in wrong direction. Storing in DB will likely be a better option, not in comma separated format, but as a table of int values.

Storing data in session will reduce scalability significantly. You might start having OutOfMemory exception and wondering why this is happening.

So my suggestion is read from DB when needed, apply appropriate indexes and it will be very fast.

The way you are heading is:

Day #1, 1 user - Hmm, should I store data in Session, why not. Should work fast. No need to query DB. Also easy to do.

Day #10, 5 users - Need to store another data structure, will put this to the session too, why not? Session is very fast.

Day #50, 10 users - There is a control that is haeavy to render, I will make it smart, render once and than put to the Session, will reuse it on every postback.

Day #100, 20 users - Sometimes the web site slow, don't know why. But it is just sometimes, so not a big deal.

Day #150, 50 users - It's got slow. Need better CPU and memory? We need to buy a better server, the hardware is old.

Day #160, 60 users - Got a new server, works much faster. Problem solved.

Day #200, 100 users - slow again, why? This is the newest the most expensive server!

Day #250, 150 users - application pool is getting recylced all the time. Why? OutOfMemoryException? what is this? I will google.

Day #300, 200 users - Users complain, we lose customers. I read about WinDbg, need to try using it.

Day #350, 200 users - Should we start using network load balancing, we can buy two servers! Bought server, tried to use, didn't work, a lot of dependencies on Session.

Day #400, 200 users - Can't get new customers, old customers go away. Started using WinDbg found out that almost all the memory is used by Session.

Day #450, 200 users - Starting a big project called 'Get rid of Session'.

Day #500, 250 users - The server is so fast now.

I've been there seen that. Basically my advice - don't go this way.

I would not recommend storing it in the session, since that's going to cause memory pressure. If you have a series of integers tied to a single record, it sounds like you have a missing many to one relationship - why not store the ints in a separate table with a foreign key to the original table?

Integers are of a fixed size in .NET. Assuming you store it in an array instead of a List (since you are probably not adding to or removing from it), it would take up roughly 32 bits * the number of elements. 1000 ints in an array = roughly 32000 bits, or a little under 4 KB.

An int usually takes 32 bits (4 bytes), so 1000 of them would take about 4KB.

It doesn't matter how large the number is. They're always stored in the same space.

Is this list of int's unique to a session? If not, cache it at the server level and set an expiration on it. 1 copy of the list.

context.Cache.Add(...

I do this and refresh it every 5 minutes with a large amount of data. This way it's pretty "fresh" but only 1 connection takes the hit to populate it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!