Re: what's better way to store a million keys in mem?
- From: dsjoblom@xxxxxx
- Date: 17 Jul 2006 08:09:42 -0700
John_Woo wrote:
Hi,
A application, needs to check whether a user already logined, by
looking at static var in memory.
I don't have good idea. what's doing is, use Hashmap <not sure
Hashtable is better interms of inserting/removing/finding> to store ID
<string> -- key and status <character> -- value.
question:
what the better way to implement this goal, it should support up to 1
million of keys, in terms of high inserting/removing/finding, and
happened more frequently.
If you *must* keep track of users by keeping user data in memory, this
is probably one of the quickest solutions. But it will eat up a lot of
memory.
If we assume an overhead of 8 bytes per object and 4 byte references, a
HashMap will use /at least/ 28 bytes per entry and that does not
include any data you put in. So if we assume your keys and values are
say 50 bytes on average, a HashMap will use /at least/ around 80
megabytes for 1 million entries. And that can probably be safely
rounded up to about 100 MB. If you run a 64-bit machine, you are even
worse off. While this may not sound too much, all of this memory is
memory that the main part of your application can't use.
HashMaps (by design) are also notorious for thrashing the CPU cache,
because of bad locality of data, and this is made even more painful in
java, because everything in the map is *also* a reference which
augments the problem.
I'd give a real database a try for this. There is of course a bit of
overhead, but you use a lot less memory and I guess it will also be
faster. Of course, you should benchmark and compare any solutions
suggested.
Regards,
Daniel Sjöblom
.
- Follow-Ups:
- Re: what's better way to store a million keys in mem?
- From: Vincent Cantin
- Re: what's better way to store a million keys in mem?
- From: Mark Space
- Re: what's better way to store a million keys in mem?
- References:
- what's better way to store a million keys in mem?
- From: John_Woo
- what's better way to store a million keys in mem?
- Prev by Date: Referencing the base method
- Next by Date: Re: Getting file into an array of byte..
- Previous by thread: what's better way to store a million keys in mem?
- Next by thread: Re: what's better way to store a million keys in mem?
- Index(es):
Relevant Pages
|