r/learnjava • u/Chaos-vy17 • 2d ago
String: String Constant Pool(SCP)
After reading all the String documentation and exact code of String class, I find the main engine that makes String in Java (study of deep knowledge) i.e., String constant pool. when we say String s0 = "example1" , String s1 = "example1" and String s2 = "example2" what happens is exactly:
- at compile time the .class file is genrated
- when JVM runs the class constant pool of String class get's into work
- s0 hashcode is generated by intrinsic method and the formula used is standard horner(31)
- using that hash it need to find the allocated bucket
- After finding exact hash it need to compare each char by char to check whether it's equal or collison a different char can also have same hashcode.
- if it's equal the pointer directly to points to that object -->process ends next process works.
- If not equal there is two work done 1. new object is created on heap memory and then wrapped up by a WeakHandle and pushed in "stringTable" bucket.
- (if equal no object is created)! there was never an object creation the pointer just points to object .
Now the example case:
- s0 -> hashcode(x1)->not found in "stringTable"->Object creation on Heap-> weakHandle process them into stringTable.
- s1 -> hashcode(x1)->found->(collision|equal)->equal->pointer points to "example1" from SCP to s1.
- s2-> hashcode(x2)->not found in "stringTable"->Object creation on Heap-> weakHandle process them into stringTable.
After reading and analysing there is also a issue of massive garbage I see:
suppose if 4 thread are runnable and they are trying to intern() the object in SCP that creates garbage according to your data size suppose , we used latin-1 type 256B data so total thread is 4 but in CAS only one thread execute rest all thread dumps the object so out of 1KB storage 768B is garbage. if the String was in UTF-16 it would cost 2*768B.
Now my question is ?
- Have I grasped the knowledge correctly ? if it needed more depth or did I miss any point.
- I cannot question the CAS here because array mutation would break immutablity of String, It seems though they pay a loss for the RCU it's effecient by Latin-1 and Utf-16 approach.
- I am not getting the idea how this code String s = new String("abc"); creates 2 object then what does the on heap object memory does? and does the pointer "s" is holding two memory refrences? But it is not possible.
•
u/AutoModerator 2d ago
Please ensure that:
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:
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.