Jvm optimization for string variables in methods

In the project that I am supporting, I found a java class with the "fn" method similar to the one shown below

class Test{ public void fn(){ String METHOD_NAME = "fn"; ... sysout("In " + METHOD_NAME); } } 

The program runs indefinitely, and the fn method is called continuous and very high frequency. Question:

  • Will the METHOD_NAME variable be created every time fn () is called?
  • will the JVM do some optimization so that the METHOD_NAME variable is not collected and used for garbage the next time fn () is called?
  • would there be a performance improvement if I made a public static final variable?
    (In fact, there are so many such functions that I want to know whether it is worth changing them all)

(I think the row pool plays a role here)

Thank you Kieran Mohan

+4
source share
7 answers

Yes variable METHOD_NAME will be created every time you enter a method, but a very very cheap operation (in fact, creating two variables is as expensive as creating 1).

Value (i.e. String object) "fn" will not be recreated, but will be retrieved from the pool of constant strings.

However, the expression "In " + METHOD_NAME will be recomposed and will cause the creation of a new String object each time, because it is not an expression expression compilation time ..

If METHOD_NAME static final , then this expression will also be a compile-time constant, and therefore will come from a constant pool.

+6
source

Variables are not collected with garbage - there are objects.

"fn" is a string literal, so it will be interned. It will not be garbage collection (at least as long as this ClassLoader is alive, not sure if there is one pool pool on the CL or one for the entire JVM, but it probably doesn't matter), and the same string object will be used for every call.

If you make this a public static ending, there will definitely be an improvement, since concatenation can be performed by the compiler, rather than at run time.

If you make it final in the method (i.e. still as a local variable), this may have the same effect - I'm not sure.

+3
source

"fn" will be interned. Therefore, the same object will be used again and again.

In the worst case, you can replace it with:

 String METHOD_NAME = "fn".intern(); 

Although I find it unnecessary.

Make public a static ending.

+2
source

As far as I know, METHOD_NAME - a reference to String 'fn' will be highlighted in every call to fn (). However, the String 'fn' object must be selected once, for it the String constant will be placed in the string pool.

Replacing it with a public static ending may be a good idea, but for a programming style, not for performance.

+1
source

String literals are placed in a constant pool. It makes no sense to put a line in a static finale - this behavior is guaranteed by JLS.

(and yes, the string will also be interned, although this is not particularly important for your problems)

0
source

Will the METHOD_NAME variable be created every time fn () is called?

The variable will be "created" (it is better to "set"), but the line will not (since this is an internal string located in the JVM string pool). So this is just a new link to the same line.

whether the JVM will do some optimization so that the METHOD_NAME variable will not be garbage collected and reused the next time fn () is called

The variable METHOD_NAME is just the name of the link. The specified row is likely to be in the row pool.

would there be a performance improvement if I made the variable public static final?

Maybe, but I would prefer to ignore it, as it would be micro-optimization.

In fact, in order to achieve a slight increase in productivity, you should think about the need to print a log statement every time, especially in a production environment where it might be too verbose.

0
source

ya go to the public static finale, which will increase the reformation.

-2
source

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


All Articles