ABONAMENTE VIDEO REDACȚIA
RO
EN
NOU
Numărul 150
Numărul 149 Numărul 148 Numărul 147 Numărul 146 Numărul 145 Numărul 144 Numărul 143 Numărul 142 Numărul 141 Numărul 140 Numărul 139 Numărul 138 Numărul 137 Numărul 136 Numărul 135 Numărul 134 Numărul 133 Numărul 132 Numărul 131 Numărul 130 Numărul 129 Numărul 128 Numărul 127 Numărul 126 Numărul 125 Numărul 124 Numărul 123 Numărul 122 Numărul 121 Numărul 120 Numărul 119 Numărul 118 Numărul 117 Numărul 116 Numărul 115 Numărul 114 Numărul 113 Numărul 112 Numărul 111 Numărul 110 Numărul 109 Numărul 108 Numărul 107 Numărul 106 Numărul 105 Numărul 104 Numărul 103 Numărul 102 Numărul 101 Numărul 100 Numărul 99 Numărul 98 Numărul 97 Numărul 96 Numărul 95 Numărul 94 Numărul 93 Numărul 92 Numărul 91 Numărul 90 Numărul 89 Numărul 88 Numărul 87 Numărul 86 Numărul 85 Numărul 84 Numărul 83 Numărul 82 Numărul 81 Numărul 80 Numărul 79 Numărul 78 Numărul 77 Numărul 76 Numărul 75 Numărul 74 Numărul 73 Numărul 72 Numărul 71 Numărul 70 Numărul 69 Numărul 68 Numărul 67 Numărul 66 Numărul 65 Numărul 64 Numărul 63 Numărul 62 Numărul 61 Numărul 60 Numărul 59 Numărul 58 Numărul 57 Numărul 56 Numărul 55 Numărul 54 Numărul 53 Numărul 52 Numărul 51 Numărul 50 Numărul 49 Numărul 48 Numărul 47 Numărul 46 Numărul 45 Numărul 44 Numărul 43 Numărul 42 Numărul 41 Numărul 40 Numărul 39 Numărul 38 Numărul 37 Numărul 36 Numărul 35 Numărul 34 Numărul 33 Numărul 32 Numărul 31 Numărul 30 Numărul 29 Numărul 28 Numărul 27 Numărul 26 Numărul 25 Numărul 24 Numărul 23 Numărul 22 Numărul 21 Numărul 20 Numărul 19 Numărul 18 Numărul 17 Numărul 16 Numărul 15 Numărul 14 Numărul 13 Numărul 12 Numărul 11 Numărul 10 Numărul 9 Numărul 8 Numărul 7 Numărul 6 Numărul 5 Numărul 4 Numărul 3 Numărul 2 Numărul 1
×
▼ LISTĂ EDIȚII ▼
Numărul 41
Abonament PDF

Common misconception: How many objects does this create?

Peter Lawrey
CEO @ Higher Frequency Trading Ltd



PROGRAMARE

A common question is how many objects or how many Strings does a section of code create. Often the answer is not what you think nor should you really need to know.  It is useful to have an idea of when an object is created but there is so many other factors which are often far more important to consider which can mean the total number for an application is not what you think.

String is a not a single object

A String wraps a char[].  This means that when you see a new String there could be a new char[] also involved. If you do + with a String it could use a StringBuilder (from Java 5.0) which also wraps a char[].  This means that usually there is more char[] created in Java than String objects. Sometimes char[] is the most common object type in a JVM.

String literals are still Strings

A common misconception is that String literals don't count.  They don't add to the total after the code has been run at least once, however most of the time the question is about code which is run once. i.e. String literals still count.

Another common misconception is when String literals get loaded.  In Java 6 and earlier they were loaded when the Class is loaded, however they are now (Java 7+) loaded when they are first used. This means that a section of code where String literals appear for the first time will create new String objects.

The JVM uses Strings, lots of them.

The JVM uses Java code and this uses Strings.  The ClassLoader to load your class uses Strings.  The name of the class you want to load is a String as are all the System properties and all environment variables which are created so you can run your program are all Strings, both the values and the key names.

Let us consider a Hello World program and see how many Strings are created so this program can run.  Is it 0, 1 or 2 Strings, see if you can guess how many are actually created..

public class HowManyStrings {
    public static void main(String[] args) throws IOException {
        System.out.println("Hello world");
        System.in.read();
    }
}  

This program stops on System.in.read(); allowing me to take a dump of the heap.  The utility jmap can give a histogram count of the number of objects currently on the heap, assuming there has been no GCs this will be the number created.

As you can see, the number of Strings was 2490.  If I had a few more environment variables or a different update of Java it would be a different number.

In short, if you are arguing over 2 to 5 String in the code you can see, when the code is run once, you may be missing most of the Strings.

But what if I call the code lots of times?

If you are talking millions of times, it is likely to matter, but here is the thing.  The JVM will optimise code which called this many times and it can do two things.

Dead Code Elimination

Code which the JIT detects doesn't do anything useful can be dropped.  The JIT is pretty good at this and most likely the example you are looking at doesn't do anything useful either.  However in real world code, hopefully it does something useful which is where the next optimisation is useful.

Escape Analysis

The JIT can look at a method (or what the method would look like after everything it calls has been inlined) and see if an object escapes the method.  If it doesn't escape the method it can be placed on the stack, or effectively have it's fields unpacked onto the stack. This means no object is created on the heap, and in fact the object header doesn't even have to be created, all it's fields, possibly none of it's fields need to be created.  In short, just because you see new String in the code doesn't mean the JIT has to actually create an object, provided it makes no difference to the result (unless you are counting the number of objects created)

Conclusion

The number of Strings created by even a trivial application is likely to be far more than you can imagine a use for, but called enough times and you might find that a method no longer creates any objects at all.

NUMĂRUL 149 - Development with AI

Sponsori

  • Accenture
  • BT Code Crafters
  • Accesa
  • Bosch
  • Betfair
  • MHP
  • BoatyardX
  • .msg systems
  • P3 group
  • Ing Hubs
  • Cognizant Softvision
  • Colors in projects