The best way is to avoid creating objects as much as possible. Not only does the creation of an object have its costs, but it also cleans them after garbage collection.
Some ideas: try to compress your data into primitive types instead, replace simple structural objects with pre-allocated parallel arrays of simple types, make your objects mutable and reset them and reuse them as soon as they are no longer needed (using a pool or ad- hoc), use Javolution to place structured data in pre-allocated bytes / arrays. If you must create new objects, avoid Java collections - they have a lot of overhead (both in memory usage and in allocating objects), try using arrays or Trove.
Perhaps you can also simplify your logic so that you don't need so many objects in the first place.
In any case, do the profiling so that you know that you are optimizing what really is your bottleneck. Often, performance hotspots are in different places than intuition says.
source share