I have an application that uses a ton of String
objects. One of my objects (lets call it Person
) contains 9 of them. The data that is written to each String
object is never written more than once, but will be read several times after. There will be several hundred thousand or so Person
objects at a given time and many of these Person
objects will share first name, last name, etc...
I am trying to think of immediate ways to reduce the amount memory that is consumed by the Person
object but I am no expert when it comes to how Java manages its memory underneath.
Before I go down this rabbit hole, I would like to know what drawbacks there would be if I went down these paths and if it even make sense in the first place:
- Using
StringBuilder
orStringBuffer
solely because of thetrimToSize()
method which would allow me to reduce the number of allocated bytes used in the string. - Store the strings as
byte[]
array's and provide a getter that would convert thebyte[]
toString
and a setter that would acceptString
and convert tobyte[]
- data is being read quite a bit, so would this be too expensive? - Create a hash table for (lets just say) "names" that would prevent duplicate allocations (using a pointer) for the same name over and over (there could be thousands of names with 10+ characters).
Before I pointlessly head down any of these roads, does it make sense to do? Maybe Java is already reducing String
allocations and checking for duplicates?
I don't mind a good read either. I have found some documentation but nothing that explores to this depth.