Java memory performance

Regarding memory usage and instantiating a variable, which is better or not the difference:

it

for(int i = 0; i < someValue; i++) { Obj foo = new Obj(); Use foo..... } 

Unlike:

 Obj foo; for(int i = 0; i < someValue; i++) { foo = new Obj(); Use foo..... } 
+4
source share
6 answers

There is no difference. Any potential difference in memory usage will be optimized by the compiler.

If you compile (using javap -c ) two examples and compare the bytecode, you will find that the bytecode is the same. Of course, this depends on the version of the JVM. But since this example is so trivial, you can probably safely assume that none of them has more memory efficiency than the other.


Example 1:

the code:

 public class example1 { public static void main(String[] args) { for (int i=0; i<10; i++) { Object a = new Object(); } } } 

Bytecode:

 public class example1 extends java.lang.Object{ public example1(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: istore_1 2: iload_1 3: bipush 10 5: if_icmpge 22 8: new #2; //class java/lang/Object 11: dup 12: invokespecial #1; //Method java/lang/Object."<init>":()V 15: astore_2 16: iinc 1, 1 19: goto 2 22: return } 

Example 2:

the code:

 public class example2 { public static void main(String[] args) { Object a; for (int i=0; i<10; i++) { a = new Object(); } } } 

Bytecode:

 public class example2 extends java.lang.Object{ public example2(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: istore_2 2: iload_2 3: bipush 10 5: if_icmpge 22 8: new #2; //class java/lang/Object 11: dup 12: invokespecial #1; //Method java/lang/Object."<init>":()V 15: astore_1 16: iinc 2, 1 19: goto 2 22: return } 
+11
source

Both will work the same with the modern JVM, and optimizing the compiler will make them the same.

Ignoring compiler optimization and modern JVM, it is better to use the 1st approach.

+6
source

The first example is of the greatest importance from the point of view of the volume of the object, but both of them should be effective in terms of memory than the others.

+5
source

There is no difference between your examples as indicated in other answers. But a question of interest: if your example is slow, what should you do?

If you want to reduce the time spent on garbage collection / assembly in a critical section with a high degree of criticality, consider reusing objects instead of allocating new objects at each iteration.

 foo = new Obj(); for(int i = 0; i < someValue; i++) { foo.init(i); Use foo..... } 

From java performance tuning (old book, but the same is true in modern jvm: s and in .NET clr)

... objects are expensive to create. where it is wise to reuse the same object, you have to do it. You need to know when not to call new. One pretty obvious situation is when you have already used an object and you can drop it before you create another object of the same class. You should look at the object and consider whether it is possible to reset the fields and then reuse the object, rather than throwing it away and creating another. This can be especially important for objects that are constantly being used and discarded.

+5
source

Optimizing the compiler will make them the same. However, I prefer the first approach better if you ignore compiler optimization. This is more important, and even Joshua Bloch suggests doing this in Effective Java (great reading).

+3
source

The compiler should optimize this for you. I would prefer the first one in a second, as it is more readable. Allowing Object foo have more volume can be a source of confusion.

+1
source

Source: https://habr.com/ru/post/1414535/


All Articles