What does Java memory management accomplish in an assignment of a previously allocated variable in memory?

Asked

Viewed 197 times

2

I would like to better understand what Java memory management does in the following situation.

Knowing that I am suffering from performance problems, I am trying to take the utmost care not to make the situation even worse, in that occurred the need to perform the following method:

public List<List<Object>> getValues() throws DataNotFoundException {
    List<List<Object>> values = new ArrayList<>();

    for (SurveyActivity surveyActivity : this.surveyActivities) {
        this.recordsFactory = new SurveyActivityExtractionRecordsFactory(this.surveyForm, this.headersFactory.getHeaders());            
        List<Object> resultInformation = new ArrayList<>();
        this.recordsFactory.getSurveyBasicInfo(surveyActivity);
        this.recordsFactory.getSurveyQuestionInfo(surveyActivity);
        resultInformation.addAll(new ArrayList<>(this.recordsFactory.getSurveyInformation().values()));
        values.add(resultInformation);          
    }
    return values;
}

Note that the attribute recordsFactory is receiving a new instance for each entry in the loop, this decision-making should be, because we need to "clear" the object instance for each entry in the loop, as the Java memory manager will handle this situation?

It will allocate this new object in the same memory space previously?

Knowing that I need to "clean up" this instance, there is a better alternative?

Is there any article where I can find more information about how the memory manager works?

  • Because it says "(...) this decision-making is necessary, because we need to "clean" the object instance for each entry in the loop (...)". I ask because it seems to me that values passed to the constructor do not change during the loop. So, the object recordsFactory instantiated has a state equal to the previous one ("is always the same"). Unless getSurveyBasicInfo() and getSurveyQuestionInfo change the state which would be "wrong".

  • @ramaral inside the Surveyactivityextractionrecordsfactory class there is a map with values that for each entry in the loop I need to ensure that there will be no garbage, using the same instance, there were cases where incorrect values appeared. At that point then I made the decision to perform a new for each entry in the loop, my question turned out to be whether this decision making ended up affecting the performance.

  • Without knowing the class it is difficult to speak. However it seems to me that you should rethink its implementation. The fact of having methods called getQualquerCoisa that return void is an indicator that something could be improved.

  • Some doubts are still blank, but I will assess your response as positive.

  • @Emanoel say what.

1 answer

4


Every time you wear one new is creating a new object in the heap (I don’t know if this will change in Java 10, which will be allocated in stack). It’s that simple. And every new object will be allocated where it’s best at that time. It’s sure to be another location and press the garbage collector.

If the object allows and no longer needs the data you can clean it (reset all data, including the object graph hanging on it) instead of creating a new one. Or at least exchange all your data, just be careful not to leave trash from the previous one. In complex objects this can be very complicated.

Depending on the case you can think of a completely different algorithm.

In fact if you have many items to process this algorithm will cause memory damage.

Some answers are not about Java, but it is either the same or gives an idea:

  • What do you mean "clean it up"?

  • Reset all object data. What may not be possible.

  • I remembered I have to finish an answer there :)

  • Yeah, back and forth I remember her :)

  • I’m wrong if I say that "Zero(null) all object data." little will change the pressure in the collector, which may even put more pressure?

  • But in an assignment as occurred above, the new object will not be stored in the same memory space as the previous object?

  • In my particular problem, there is a map in the Surveyactivityextractionrecordsfactory class that ends up with garbage if I use the same instance. When you talk about cleaning, knowing that it is a map of key and value, is there a better way to perform this procedure than to give a new one for each element?

  • @ramaral It is not, but it is also :) It depends. First it’s not always possible to reset everything, its API has to leave, remember there are things you may not have access to. If the object is by value changes nothing, if it is by reference, it will not put more pressure, comparing it with always allocating a new object, but it will have much less advantages, because a part of the object graph contained in it will still have new allocation. I mean, it’s like the problem of clone(), to clone everything (deep) need to traverse the entire graph and deal with the cycles.

  • It is not enough to zero only on the surface, it would have to zero, and not to allocate new to the entire graph, and all objects need to allow this to achieve total gain. That is, it is complicated. If the person can it would be good to create an object that deals with what would be like a pool. For trivial applications GC is great for those who need memory optimization I have become increasingly convinced that semi-automatic management with the aid of good tools can give better results.

  • @Emanoel would need to go deeper, it really is a case that complicates a little, but I can’t tell you the result without testing. Every time you wear one new an entirely new object is created and will occupy space in memory by itself.

  • While getValues(); As long as the instance does not stop using them the objects remain in the Heap, however each object of the for will be removed by the GC if their cycle ends. The best way to see where you are causing the slowness is by using a profile.

  • I had understood that I would reset and then create a new one and that the alternative would be to "exchange all your data" (reuse the object).

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.