Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long static Strings in short-lived objects

Tags:

java

This might be a stupid question (or just make me look stupid :)), however I would be interested in how to work with long String objects in the context of short-lived objects.

Think about long SQL queries in cron job or anonymous, command or function-like classes. These are very short-lived classes and even will use these long Strings once in their lifetime for most of the time. What is better? To construct a String inline and let it be collected with the instance, or make it static final anyway and let them sit in the memory useless until the classes next instantiation?

like image 496
user1405469 Avatar asked Apr 17 '13 08:04

user1405469


People also ask

What are short lived objects?

An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation. Newly allocated objects form a new generation of objects and are implicitly generation 0 collections.

What are the short lived objects in Java?

The Java heap is roughly broken up into 3 parts, permanent, old (long lived) generation, and young (short lived) generation. Young gen is further broken up into S1, S2 and eden. These are just heaps. Most objects are created in the young gen.

How are strings stored in memory?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

Are strings on stack or heap?

In Java, strings are stored in the heap area.


2 Answers

Well, there's only so much control you can have over what happens to the String.

Even if you create it inline, that String will most probably be added to the String constant pool of the JVM, and will be reused when you declare it again, so in practice, you'll probably be reusing the same String object either way.

Unless the String is so huge that it has an impact on your application's performance, I wouldn't worry about it and choose the option that seemed more readable to me.

If that String will be used only in one particular point of the code, inside a method, I would declare it inline, I prefer to have my variables in the smallest scope that I can, but opinions here may vary. If there is no change whatsoever, and it seems to make sense in your particular use case, by all means declare the String as static, again, I doubt it will affect performance.

like image 188
pcalcao Avatar answered Oct 20 '22 06:10

pcalcao


String constants go into the constant pool of a class, and cannot be optimized away, i.e. are handled sufficiently well.

Creating long strings one does not do statically. For SQL use prepared statements with a ? place holder. The same holds for strings with placeholders: use MessageFormat.

To be explicit. The following does not cost anything extra:

static final String s = "... long string ...";
like image 43
Joop Eggen Avatar answered Oct 20 '22 06:10

Joop Eggen