10

I'm not certain if this question is language independent or not so I'm going to just ask this as a Java question. If you have a loop like

for (int i = 0; i < 9; i++) {
    Object obj = new Object;
    // fill object
    // do something with data
}

Is new space in memory being created through every iteration of the loop? If so, is the old space being disposed of? If not, is it more efficient to do something like this

Object obj = new Object;
for (int i = 0; i < 9; i++) {
    // fill object
    // do something with data
}

Edit: Updated code to better exemplify the question

Steve
  • 4,446
  • 7
  • 35
  • 50
  • I don't think you should create String on the heap. The Best approach is to create a String variable like String object = null , this is nothing but a pointer to the string. Each time you are iterating you are creating String objects on the heap and this approach in this slower than the previous one. Hope that helps – allthenutsandbolts Apr 22 '12 at 22:55
  • 1
    These two loops do completely different things. – Louis Wasserman Apr 22 '12 at 22:56
  • 1
    There is similar [question](http://stackoverflow.com/q/8803674/617996) already.... – PrimosK Apr 22 '12 at 23:02
  • I tried looking @PrimosK but couldn't find it, thanks for the heads up – Steve Apr 22 '12 at 23:20
  • Eh? The link he provided works for me. – user207421 Apr 23 '12 at 01:42
  • @ejp I meant I tried looking before posting the question, the link works for me as well thanks – Steve Apr 23 '12 at 08:31

3 Answers3

9

Short answer: just ignore such things while developing in Java, even because most of these problems are black boxed and managed by the JVM itself.

The garbage collector will know what to do and it will try to do the best thing according to the situation.

In addition your specific examples don't make much sense to me.. could you elaborate it further?

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I take it then this same answer wouldn't apply to languages that don't have garbage collection like c++ – Steve Apr 22 '12 at 23:22
  • 1
    No. The same answer applies in C++ too. The principle is to not optimize stuff until you have good evidence that it is worth the effort. (Unfortunately, a lot of C / C++ programmers ignore this principle ... ) – Stephen C Apr 23 '12 at 01:00
  • When you say ignore things that means to just place the object creation in the inner most appropriate scope like here http://stackoverflow.com/questions/8803674/declaring-variables-inside-or-outside-of-a-loop – Steve Apr 23 '12 at 08:34
7

The basic answer of "let the garbage collector figure it out" is correct, but you should also understand that the effect of these 2 statements:

String object = new String();
object = array[i];

is to (1) create a new (empty) String, and assign it to "object", followed by (2) assign the value of array[i] to "object".

The assignment in (2) overwrites what you did in (1), so the first String you created is never used. The garbage collector will eventually reclaim the storage, so you don't have to worry about it being lost, but it's still a needless allocation; you could just start out with String object = null;

Also note that Strings are immutable in Java, so once you allocate a String object you can't change its value -- you can make a String variable refer to a new object, but you can't modify an existing one.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
1

Why don't you run some tests and watch the results? It's true that garbage collector works, but you can't know it works for the best. Working with huge portions of data, this kind of thing may effect your efficiency.

I think this link would help to trace what you need.

İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64