akhter wahab wrote:When I run the applications through Eclipse, I see the memory usage and the heap size increase a lot. Upon further investigation, I see that they were creating an object over-and-over in a loop as well as other things.
What do you mean by "a lot"? Premature optimization is bad.
akhter wahab wrote:I started to go through and do some clean up. But the more I went through, the more questions I had like "will this actually do anything?"
What happened when you ran it through a profiler?
akhter wahab wrote:
For example, instead of declaring a variable outside the loop mentioned above and just setting its value in the loop... they created the object in the loop. What I mean is:
versus
Am I incorrect to say that the bottom loop is better? Perhaps I am wrong.
You are incorrect. First off, you'll be keeping the references around after the loop. And you're still creating the same number of instances of someClass (which should really be SomeClass). I think what you mean is this:
akhter wahab wrote:Also, what about after the second loop above, I set "something" back to null? Would that clear out some memory?
Maybe. Maybe not. Up to the garbage collector. What happened when you profiled it?
akhter wahab wrote:In either case, what are some good memory management best practices I could follow that will help keep my memory usage low in my applications?
Don't prematurely optimize your code.