Display Name
Display Name

Reputation: 8138

Excess instantination of the same object?

Consider following code as totally simplified example of the idea:

public class Main {
    public static void foreach(int[] x, intVoid action) {
        for (int i = 0; i < x.length; ++i) {
            action.apply(x[i]);
        }
    }

    public static void main (String[] args) {
        int[] h = new int[] {2, 3, 5, 7, 11, 13};
        for (int i = 0; i < 10; ++i) {
            foreach(h, new intVoid() {
                public void apply(int x) {
                    System.out.println(x * 2);
                }
            });
        }
    }
}

interface intVoid {public void apply(int x);}

In the for loop we call foreach with absolutely identical arguments from logical point of view, since the anonymous implementation of our interface doesn't depend of anything in the context. The question is - will it be instantinated 10 times or only once? Or, similar, will it be equivalent for the following code:

public class Main {
    public static void foreach(int[] x, intVoid action) {
        for (int i = 0; i < x.length; ++i) {
            action.apply(x[i]);
        }
    }

    public static void main (String[] args) {
        int[] h = new int[] {2, 3, 5, 7, 11, 13};
        intVoid action = new intVoid() {
            public void apply(int x) {
                System.out.println(x * 2);
            }
        };
        for (int i = 0; i < 10; ++i) {
            foreach(h, action);
        }
    }
}

interface intVoid {public void apply(int x);}

I sometimes had been in such situations, where it's very convenient to define an implementation exactly in place where it's needed, but sometimes I also need to be sure that there will be no attempt to create the same object multiple times. If there is an optimization in the runtime, then I'm interested in how this is handled in different implementations, especially what will happen if I convert such code to be run in Android's Dalvik VM.
I know that I can test this all myself, but I'm wondering if someone has already shed some light on this question.

Upvotes: 0

Views: 214

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503869

The question is - will it be instantinated 10 times or only once?

10 times, because that's what you've told it to do. I'd be surprised to see that the JVM (or Dalvik) noticed that apply had no side-effects and that the object had no state, and therefore you didn't need to create separate instances. It's possible - Hotspot's pretty impressive - but it seems an unlikely optimization to me.

Of course, creating each object is probably cheap - but it would still be better to create one object as per your second code snippet.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533880

It is instantiated once for every new performed. It is possible escape analysis will eliminate it after it has been JITed e.g. after 10,000 times but I wouldn't assume that it does.

If it's smart enough not to create separate instances, it's smart enough to eliminate it which is better.

Upvotes: 3

Related Questions