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

6
  • 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 Commented Apr 22, 2012 at 22:55
  • 1
    These two loops do completely different things. Commented Apr 22, 2012 at 22:56
  • 1
    There is similar question already.... Commented Apr 22, 2012 at 23:02
  • I tried looking @PrimosK but couldn't find it, thanks for the heads up Commented Apr 22, 2012 at 23:20
  • Eh? The link he provided works for me. Commented Apr 23, 2012 at 1:42

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

I take it then this same answer wouldn't apply to languages that don't have garbage collection like c++
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 ... )
When you say ignore things that means to just place the object creation in the inner most appropriate scope like here stackoverflow.com/questions/8803674/…
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.