0

We have some legacy code that was written in the early 2000s that was running fine till now, except when we added more users to the system it had daily OutOfMemory exceptions

We are going to redo the project from ground up, but we want to best use the existing code base.

The code has thousands (yes not the best design) of HashMaps that are only modified in a loop on creation and after that remain readonly.

When i run Memory analyser it says that there is 15% of the heap consists of unused space in HahsMaps.

For these instances if we use reflection to make the entry set accessible and resize it so the size is equal to the actual needed, will iterators fail?

After initial construction these maps are read only caches. We are running the code on Java 6 and few instances on Java 8. wont be upgrading the JVM version, in another 10-12 months the new code will be in production and replace the current.

I know we can give a better initial size when creating the hash map but that is a lot of work compared to one function that resizes a map based on current size. ALso most of the maps are part of libraries (that i do not have access to source code) and these do nt expose the the constructor that accepts size. they call default constructor of their internal HashMap only.

tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • 2
    why resize by reflection? I don't get it, resize will happen anyway when you insert more elements, but in general do you know the approximative size of the resulting map? otherwise this is a pretty vague question to be honest – Eugene Dec 22 '18 at 09:28
  • 1
    HashMaps have a resize functionality, according to a preset ratio between the size of the table and how many items are in it. Is this done to increase performance? – yuvgin Dec 22 '18 at 09:34
  • 2
    @YuvalG it's not that easy, but in general resize is there to shuffle entries and distribute them more uniformly across buckets, for faster searches, so yes it is a performance thing. It's just that resize itself is a bit expensive, thus choosing the initial size sometimes is very important – Eugene Dec 22 '18 at 09:40
  • Clearly the initial capacity is important (especially in a read only map), but this can be given as a parameter upon HashMap creation.. Thus I didn't understand the need for reflection – yuvgin Dec 22 '18 at 09:44
  • 1
    @YuvalG even that [is not that easy for HashMap](https://stackoverflow.com/questions/52692803/hashmap-rehash-resize-capacity) – Eugene Dec 22 '18 at 09:50
  • we are getting out of memory errros. we have 100,000 hash maps! per memory analyzer heap dump report s Most are created, few elements (10-100) are inserted and after that read only. Do not want to impl a distributed cache right now – tgkprog Dec 22 '18 at 10:01
  • @YuvalG yes true thing is that there is a class in a shard libray, one of its fields extends HashMap that does not expose this consrtructor and i do not have access to this shared library. So cant change that code. This class is closely mixed with the main code not easy to remove it. so iw as hoping for a minimally invasive function that takes a hashmap, resizes it so it takes less space in Heap. the caller would make sure that these are the read only hash maps. Wondering about the table (grows by power of 2) and the entries field in HashMap. – tgkprog Dec 22 '18 at 10:18
  • As you're already reengineering the whole solution, I'd advise to drop that old library that makes so bad use of `HashMap` and implement something new from scratch. Maybe it will take some effort, but it will be worth it. Your proposed solution is far from being a solution, it would be just a hack that depends on undocumented implementation details that might change even in a minor version update. Besides, it would be incredibly expensive, i.e. you can't trim an array, you'd need to create a new one with less capacity and copy all the elements. Also, you'd break hashing for sure. – fps Dec 23 '18 at 00:29
  • @federico-peralta-schaffner yes but we need to keep the app running & users happy until the new version is ready. anyway i found out things about hash maps will publish them in a few days. we are doing a few things to reduce memory and this i hope will cut it by 10% – tgkprog Dec 23 '18 at 20:42

0 Answers0