• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating vs Resetting  RSS feed

 
Micho Lee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was wondering how efficient the java garbage collector was at cleaning up unlinked objects. If I have an object I reuse over and over again but just reinstantiate it with each round of the program's execution instead of resetting particular variables back to their initial value, does it make a large performance difference?
In other words, as a simple example:
1.
for (int nCounter = 0; nCounter < 10; nCounter++)
{
myObject1 = new myObject();
}
2.
for (int nCounter = 0; nCounter < 10; nCounter++)
{
myObject1.init()
}
Would 1. be a lot worse than 2.? The init() function would just rest all the variables to their initial values.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how large and complex the objects are. For small objects with just a few member variables, it actually makes so little difference that it's best to just do whatever's easiest. For large, complex objects (like a Swing component, for example) reuse is a lot faster.
Like most performance issues, the best thing to do is to write clear, straightforward code that's easy to understand, and then profile it. Find out where the performance bottlenecks are, and then fix them (which may involve reusing objects, for example -- but only if you see object creation as a measured bottleneck.)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the garbage collector is far better in removing short-lived objects than longer living objects.
But actually I wouldn't care about this too much. Far more importantly, reusing the object just opens the possibility for nasty bugs, such as forgetting to reset a field when you introduce a new one and similar things. So I wouldn't use technique until I actually had a proven performance problem, and a prove that the technique is the most straightforward way of solving it.
 
Micho Lee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice! I'd rather re-instantiate too.. to avoid bugs... but if performance becomes and issue I guess I should look into reusing the object.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
It depends on how large and complex the objects are. For small objects with just a few member variables, it actually makes so little difference that it's best to just do whatever's easiest. For large, complex objects (like a Swing component, for example) reuse is a lot faster.

Is this because of memory reallocation? My impression is that for Swing components, the savings is more from refraining from reinitializing the object, thus saving a lot of expensive initialization work the second time around.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Dew:

Is this because of memory reallocation?

More likely because of the allocation of other, more costly resources.
Database connections are also known to be rather costly.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Dew:

Is this because of memory reallocation?


"Complex" pretty much means being the root of a tree of objects. So we're comparing reusing a java.awt.Point (one object, 2 members) to reusing a javax.swing.JEditorPane (roughly one zillion interconnected objects). You can call it what you like, I guess.
In terms of allocating one single object, it's unlikely that size matters much for most values of "size". An array of a quarter-billion elements would definitely take longer to allocate than a java.awt.Point, though, as doing so would involve lots of object movement, and possibly involve some sbrk() calls.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just found a very interesting article on the subject, indicating that most attempts to "help" the garbage collector are more likely to do harm.
See the link at http://faq.javaranch.com/view?PerformanceOfGarbageCollection
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!