Let's say I have an application that maintains relationships between objects - think of a parent-child relationship tree. The lineage of these objects are critical, so maintaining references from Object A <-> Object B <-> Object C is very important. I cannot let the GC clean up any of these objects. Access to the data is very time sensitive and this tree can grow very, very large. In order to have immediate access to this data, I've obviously had to do it at the expense of memory and we all know what that inevitably leads to...

Before I explore solutions like Spark, Hadoop, databases, caching, or any other storage / data processing engines, I would like to better understand how Java is managing memory and if there are immediate solutions that might squeeze a little more juice out of my current design.

Lets just say each object represents a Person. It contains details about that person such as first / middle / last name, date of birth, places they have visited over the last decade, make / model of current vehicle, etc... Basically a variety of String, Integer, Boolean, data types. And then of course you have your Parent and Children which are references to other Person objects.

When I've decided a Person object or any of its children have not been inquired about in X amount of time, I would like to compress these objects down to something much more manageable (in regards to memory) but still keeping context about the person and its relationships for speed.

So before I head down this path (requires quite a bit of refactoring), I am trying to think of the best possible solution - if at all possible.

Would something like taking all fields and storing them in a Byte[] array with serialization / deserialization methods to compress & uncompress the data when they need to be read reduce any notable amount memory? I need a way to maintain the relationship of Object B so that Object A and Object C know about one another while keeping the information of Object B to an absolute minimum but still readily available.

Hopefully I've provided enough context to my question and wasn't too vague. If I have been, I'd be happy to elaborate.

  • @RobertHarvey I understand what you are saying but I've 1) already explained a broad picture with a possible solution, 2) I've offered to provide more details if needed, and 3) explained why I am not moving to other solutions just yet. But I appreciate your constructive feedback. Dooley noted. – user0000001 Jun 5 '18 at 22:12
  • I'm not sure that object pooling is really what you are after. It would be really helpful if you could tell us a bit more about what your actual problem is, rather than your proposed solution to the problem you are having. – Robert Harvey Jun 5 '18 at 22:24
  • The problem is memory usage. I am trying to figure out a good solution (before heading down the path of caching or distributed processing) to reduce object data that cannot be forgotten. The problem is, when the application runs for a few days it will inevitably run out of memory. – user0000001 Jun 5 '18 at 22:36
  • Apparently there is already a modern answer for my question. – user0000001 Jun 5 '18 at 23:53
  • Is the Flyweight pattern suitable for your purpose? Using it, you can save some space. en.wikipedia.org/wiki/Flyweight_pattern – Thomas Deniffel Jun 6 '18 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.