1

I would like to know what happens to the memory allocated to objects that are dependent on static variables and what is the best practice for this, example:

private static final int lineSeperator = System.getProperty("line.separator");

public String concat(String a, String b){
    String concat = a + lineSeperator + b;
    String concat2 = concat + "2";
    return concat2;
}

What would happen in memory to concat and concat2, will they be garbage collected or will they stay alive for ever since they are using a static variable?

3
  • 3
    They'll be garbage collected. Well, concat will be some time after the method returns. But concat2 is returned from the method, and so will be kept in memory so long as there are strong references to the object. As for the variables themselves (not the objects the reference), they'll be deleted when the stack frame is (when the method returns). Commented May 29, 2022 at 7:36
  • 1
    The same principles apply to all Java objects. If an object is reachable then it won't be garbage collected. If it is not reachable, then it is a candidate for garbage collection. In short, just let the garbage collector deal with it. (Anything you try to do to help will probably have no measurable benefit, and may make things worse.) Commented May 29, 2022 at 7:40
  • 1
    "what is the best practice for this" - Read No Best Practices Commented May 29, 2022 at 7:41

2 Answers 2

4

What would happen in memory to concat and concat2, will they be garbage collected or will they stay alive for ever since they are using a static variable?

The String in the concat variable will be eligible for garbage collection when this method returns since it will no longer be reachable.

The lifetime of the String in concat2 that is being returned (modulo the compilation error!!) will depend on what the caller does with it.

The + operator will generate a new String. The fact that it contains characters from an existing String that (in this case) is referenced from a static variable makes no difference. The characters are copied into the new String as it is being formed.


what is the best practice for this

  1. There is no "best practice". Read No Best Practices.

  2. Given that there are no garbage collection implications for concatenating static strings, you don't need to do anything.

  3. As a general rule, you can just let the JVM optimize string concatenations as it sees fit. It will probably produce code that is as good as anything you can achieve. Possibly better ... in recent JVMs.

    The only situation where it can be helpful to intervene is if you are constructing a large string by looping over some data structure; e.g.

    String res = "";
    for (String member: someLargeList) {
        res = res + " " + member;
    }
    

    For this example may be advisable to use a StringBuilder and append calls to build the string. Better still use StringJoiner or equivalent.

    But it is NOT advisable to try to optimize string concatenations in general; see above.

Sign up to request clarification or add additional context in comments.

Comments

0

They will be eligible for garbage collection as soon as the last statement of method concat() is executed.

By the way, your above concat() method will not compile as it is returning String value when it's return type is void.

1 Comment

It was just an example of usage of the static variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.