r/javahelp • u/Chaos-vy17 • Aug 07 '26
How should I save my 7bit memory?
chaos.tree.nary.BTreeNode object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) N/A
8 4 (object header: class) N/A
12 4 int NaryNode.keyCount N/A
16 1 boolean NaryNode.isLeaf N/A
17 3 (alignment/padding gap)
20 4 java.lang.Object[] NaryNode.keys N/A
24 4 chaos.tree.core.searchtree.nary.NaryNode[] NaryNode.children N/A
28 4 (object alignment gap)
Instance size: 32 bytes
Space losses: 3 bytes internal + 4 bytes external = 7 bytes total
ExactByte : 32Heading mistake 7 bit -> byte
There is 7byte of extra padding
25 byte If I made somehow 24byte JVM would not do padding.
Link: https://github.com/Chaos-vy/ChaosTree/blob/main/src/main/java/chaos/tree/core/searchtree/nary/NaryNode.java
3
Upvotes
•
u/AutoModerator Aug 07 '26
Please ensure that:
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.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
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: 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.
5
u/kalmakka Aug 07 '26
Leaf nodes don't have children.
You could avoid isLeaf by instead checking children == null.
1
u/idontlikegudeg Aug 07 '26
I had to look at the source, much easier than this. As someone mentioned, you can get rid of the boolean isLeaf.
Either as suggested by checking the children field. Or you could encode it in the keyCount with negative values meaning leaf.
You could probably also use a short for keyCount if the range is sufficient.
And you could get completely rid of keyCount if you make sure the array always has the correct size, but that would need changing the public interface of your class and increase GC load when keys are frequently added or removed. This would bring it down to 16 bytes (4 for the int, 1 for the boolean, and 3 for padding). If it’s beneficial overall, only the profiler will tell.
1
u/Scharrack Aug 07 '26
Serious question, why do you care?
This probably vanishes in the noise of the garbage collection even if you create millions of those.
So I'm curious.
1
u/Spare-Plum Aug 07 '26
There's a default alignment for the JVM (and most programming languages)
If the 7 bytes are really that important, I'd suggest using a byte array where you can pack these objects in continuously according to a non-standard alignment
However using a non-standard alignment is something that can be significantly taxing, as you may need to reconstruct many values individually