开发者

resizing arrays when close to memory capacity

So I am implementing my own hashtable in java, since the built in hashtable has ridiculous memory overhead per entry. I'm making an open-addressed table with a variant of quadratic hashing, which is back开发者_如何学编程ed internally by two arrays, one for keys and one for values. I don't have the ability to resize though. The obvious way to do it is to create larger arrays and then hash all of the (key, value) pairs into the new arrays from the old ones. This falls apart though when my old arrays take up over 50% of my current memory, since I can't fit both the old and new arrays in memory at the same time. Is there any way to resize my hashtable in this situation

Edit: the info I got for current hashtable memory overheads is from here How much memory does a Hashtable use?

Also, for my current application, my values are ints, so rather than store references to Integers, I have an array of ints as my values.


The simple answer is "no, there is no way to extend the length of an existing array". That said, you could add extra complexity to your hashtable and use arrays-of-arrays (or specialize and hard-code support for just two arrays).


You could partition your hash table. e.g. you could have 2-16 partitions based on 1-4 bits in the hashCode. This would allow you to resize a portion of the hash table at a time.

If you have a single hash table which is a large percentage of your memory size, you have a serious design issue IMHO. Are you using a mobile device? What is your memory restriction? Have you looked using Trove4j which doesn't use entry objects either.


Maybe a solution for the problem is: ->Creating a list to store the content of the matrix (setting a list for each row and then free the memory of the array in question if possible, one by one); -> Create the new matrix; ->Fill the matrix with the values stored on the list (removing 1 element from the list right after copying the info from it.

This can be easier if the matrix elements are pointers to the elements themselves. This is a very theorical approach of the problem, but I hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜