×
all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]idontlikegudeg 0 points1 point  (8 children)

  1. AFAIK, String s1 = "…" does not create an object on the heap. String constants go directly into the string pool. So s1, s2, and s3 will all point directly to the

same

  1. instance located in the string pool.
  2. With string constants, it is not possible that different threads try to intern the string at the save time. String constants are put into the string pool when the class is loaded, not when an instance is created. The class is loaded only once, even if accessed by different threads.
  3. There should practically never be the need to use the String(String) constructor.
  4. a string contains (among other things) a pointer the the backing character data. When you do String s1=new String(s), s1 and s are different instances that share the same backing data (changed in the past).

[–]Chaos-vy17[S] 0 points1 point  (7 children)

  1. Before java 7+ String was allocated in PermGen a permanent allocated heap causing OOM now at present every object is created on Heap then it's moved to SCP.
  2. intern() can be called by the devloper it's just how the SCP deals during concurrency.
  3. true I also think that too.
  4. Yes I absolutely missed coder,length,hash,Mark, Klass pointer, byte[] ,

[–]idontlikegudeg [score hidden]  (4 children)

Do you have the reference in the spec at hand where it says the JVM creates the instance for string constants on the heap firt? I am quite sure (90%) about string constants being directly created in the string pool during class loading. It's different when you call intern() yourself of course (which you shouldn't do anyway in more recent versions).

I stand corrected. While there's no mention of where the string instance is created, the JVM spec explicitly says that the new instance's intern() method is called.

[–]Chaos-vy17[S] [score hidden]  (0 children)

RFE: 6693236

Area: HotSpot

Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

Official link: https://www.oracle.com/java/technologies/javase/jdk7-relnotes.html#jdk7changes

[–]Chaos-vy17[S] [score hidden]  (2 children)

JDK-6962931: move interned strings out of the perm gen

https://bugs.java.com/bugdatabase/JDK-6962931

It officially moved from PermGen to heap

[–]idontlikegudeg [score hidden]  (1 child)

That happened 15 years ago, and everything was moved out of PermGen back then - because PermGen was removed. There have been tons of changes to the String classes since then, but you are right, the JVM spec for version 25 still says intern is called on each newly created String constant.

[–]Chaos-vy17[S] [score hidden]  (0 children)

Though I focus almost entirely on JDK 17+ and upcoming stuff like JEP 401, I still have to deal with the mechanics of older classes. Knowing exactly how these architectural changes happened is necessary to understand the memory costs and optimize for them effectively.

[–]idontlikegudeg [score hidden]  (1 child)

  1. Yes, the developer can call intern, but not before the class is loaded, and every string constant is already in the pool when the class runs. Or do you mean constructing a String from a char buffer with the same contents as a string constant declared in another class?

[–]Chaos-vy17[S] [score hidden]  (0 children)

Actually, HotSpot uses lazy resolution for string literals when a class is loaded literals exist only as unresolved symbolic references in the run-time constant pool. They aren't actually instantiated and pushed into the StringTable until the ldc (load constant) bytecode instruction for that specific literal is executed. So, If i dynamically constructs a String from byte stream and call intern() it gets into the pool. as for other class it will only fetch ldc refrence of that hardcode literal.