Java – does it still make sense to avoid creating objects for garbage collection?

For example, in a service adapter, you might:

One There is an input data model and an output data model, which may even be immutable, have different classes, and use object mappers to convert between classes and create some short-term objects

Bay has a data model, some classes may be variable, but the same objects created for input are also sent as output

There are other use cases. You have to choose between clear code with many objects and less clear code with fewer objects. I wonder if garbage collection still has weight in this decision

Solution

I should take this as a comment as IMO, it does not meet the answer, but it is not suitable

Even if the answer is likely to be - doing anything will make your code more readable (and to be honest, I will always follow this); We have encountered this GC problem in the code base

Suppose you want to create a user graph (we have to - about half) and load all their attributes in memory and aggregate and filter them (this is not my decision), because these graphs are very heavy objects - once 16GB heap is loaded, the JVM will fail due to oom or GC, which will take a lot of pause time This is understandable - a lot of data requires a lot of memory, and you can't escape it The proposed and practical solution is to model it with simple bitsets - where each bit is an attribute and may be associated with some other data; So far, this is still unreadable and very complex A lot of transformations, a lot of data internality - you must always know what 3 bits mean, for example, there is no getter for usernameincome, you can say - you must do a lot of transformations and map them to the search table, etc., but it will keep the GC quite low, at least within our acceptable range

So unless you can prove that GC takes so much time for your application - you may be safer, just add more RAM and increase it (unless you have a leak) I still choose clear code like 99 (99)% of the time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>