Object creation for nested data structures, an avoidable hit on performance?

From Android's Performance Tips:

Object creation is never free. ... As you allocate more objects in your app, you will force a periodic garbage collection, creating little "hiccups" in the user experience.

My app periodically collects GPS data, along with some meta data. For each location request, it should add the result to a data object:

"data": {
    "timestamp": {
      "lat": "value",
      "lng": "value"
      "meta": "value"
    }
    "timestamp": {
      "lat": "value",
      "lng": "value"
      "meta": "value"
    }
    ...
}

Then after the object reaches a certain size, or when the device has better connectivity, the app will stream the full data object to my server, clear it, and re-use the empty object.

While re-using the outer data object would yield better performance, and surely isn't that difficult to accomplish, it seems impossible to re-use the inner timestamp objects. Could I re-use the inner objects that turn out to be useful in the next round of GPS data collection, and let any remaining inner objects be garbage collected?

Jon Skeet
people
quotationmark

Could I re-use the inner objects that turn out to be useful in the next round of GPS Data collection, and let any remaining inner objects be garbage collected?

Yes, potentially. But do you have any indication that it's actually a good idea to do so? How often are you collecting this data? You mentioned "periodically" earlier - that doesn't sound like you're getting a huge amount of turnover.

Micro-optimization like this can certainly be important in applications where smooth performance is absolutely critical - such as mobile games - but in many other apps it's just unnecessary, and hurts the readability and maintainability of the code.

I suggest you measure the performance of your app before making a decision either way. Is garbage collection actually hurting you? Is this type a source of these problems? (There's not much point in recycling this particular type if it's only a tiny part of your GC issues.)

people

See more on this question at Stackoverflow