Java is garbage collected. The memory allocated for the string will be freed at a feature point in time determined by the garbage collector. See Java Garbage Collection Basics for details.
Update: as commenters pointed out string interning might prevent the string from getting garbage collected. Details depend on many things like the scope of the reference, JVM and Java version.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…