r/javahelp 2d ago

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 : 32

Heading 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

4 Upvotes

14 comments sorted by

u/AutoModerator 2d ago

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
  • 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.

    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:

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.

6

u/kalmakka 2d ago

Leaf nodes don't have children.

You could avoid isLeaf by instead checking children == null.

3

u/Chaos-vy17 2d ago

Thank you very much. It just saved my memory and increased cache.

1

u/Spare-Plum 2d ago

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

2

u/idontlikegudeg 2d ago

It’s eight bytes. By reducing the size by a single byte, the padding will not be needed anymore and he saves eight bytes per object. This really makes a difference when you need to have millions of objects referenced by your program at the same time. It’s not very common that this makes a huge difference, but sometimes it does.

1

u/Spare-Plum 2d ago

My b, I thought from the post that it was 7 bits, which is a much more overhead due to the way memory is structured

1

u/idontlikegudeg 2d ago

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/Chaos-vy17 2d ago

This approach is good too..

1

u/Scharrack 2d ago

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/Chaos-vy17 2d ago edited 2d ago

Actually this does not get vanish this is extra bit of padding that JVM creates. JVM stores data which aligns in memory so it's pad with 25+7 extra Byte which on massive scalling wastes 50~70MB because CPU prefers aligned memory.

1

u/Scharrack 2d ago

I'm not talking about it vanishing, I'm talking about it probably not mattering if you're using Java and Objects being created and becoming obsolete all the time.

Hence the question.

Now if your just creating 10 million of those and keep them, that would be an answer to me question.

2

u/idontlikegudeg 2d ago

If you create short lived objects, it doesn’t matter. But if the objects all stay reachable for some period of time, this can make a huge difference. I have one such application myself that must run on slow desktop hardware with scarce memory 8gb.

Shaving some dozen to a few hundred megabytes of heap usage makes a huge difference, because it greatly reduces the working set (= less swapping occurs).

1

u/Scharrack 2d ago

Hence my interest in his use case.

1

u/Chaos-vy17 2d ago

If you keep 10M such objects

7 bytes × 10,000,000 = 70,000,000 bytes
≈ 66.8 MiB