0

Here's what I have

public void someMethod() {
  MyObject myObject = new MyObject();

  .. some more initialization code..

  myObject = methodThatRetunsMyObject(); 
  myObject.doSomething();
}

Question: Will the orphaned object creation lead to significant performance issues?

The goal is to achieve maximum transactions per second, hence my assumption is, orphaned objects would lead to GC runs which could affect throughput.

Oliver
  • 104
  • 1
  • 9
  • What is it being used for? If you can reuse it between methods, you could have it be defined in the class instead of in the method and/or make it static – phflack Oct 29 '15 at 14:48
  • Reusing the objects isn't an option, pardon me for over simplifying the code snippet. Assume arguments passed that create unique objects on every invocation. – Oliver Oct 29 '15 at 14:51
  • If it can't be reused, then that's probably one of the best options, unless you can make it use static methods instead? It sounds like you could just put the initialization into the constructor and remove the first call of `MyObject myObject = new MyObject();` – phflack Oct 29 '15 at 14:53
  • If you don't use the first instance you allocated (the code shown doesn't) then that allocation is useless and should be removed. If each instance is unique and necessary, then the question doesn't matter because correctness trumps performance. – dsh Oct 29 '15 at 14:54
  • Yes, that's what I was getting at, the first line unnecessarily creates an object, the actual object that is used down the line is the one returned by the methodThatRetunsMyObject() method. Hence the question will that unnecessary object creation on line one lead to a performance Issue. – Oliver Oct 29 '15 at 14:58
  • If the object is not being used for anything, then don't create it, just create the instance that WILL be used – phflack Oct 29 '15 at 15:01

0 Answers0