How much memory does a byte variable need in Java?
The knowledge about this seems to be shocking low.
The rules for memory usage in Java are explained.
There are hundreds of murky little things in JVM knowledge of which does not make one a better programmer. You do not design your classes based memory alignments but rather on OO principles, maintainability patterns, and so on. If you do your design based on memory alignments your code becomes garbage and should be collected.
In respect of memory alignment, he is correct --- in Java you should pay very little attention to it. Unlike C/C++, a JVM is permitted to arrange fields in an object in whatever order is best. This can eliminate much of the alignment padding that would occur in a carelessly arranged C/C++ structure. Even better, the fields of a subclass can be located in spare space within its parent (though implementation of this optimization is quite recent).
While Java does have higher minimum memory requirements than C (perhaps 8MB for non gui, 16MB for GUI apps), memory use beyond this is largely the choice of the developer and not really a function of the language. For example, many IDEs of recent design are quite large, regardless of the implementation language.
It's not only about alignment, it's also about whether you should deep inheritance (which can increase memory) or whether you should char[] instead of String objects in certain place, trading better performance (less memory usage) for a less ideal OO design.
I disagree.
It's not a good idea, to just design your classes on OO principles alone, because that would mean that you ignore resource consumption completely.
Why would you design a class, which you expect to be instantiated very often to wast memory, just because of OO principles?
There is a difference between designing class that gets instantiated very often (which should be a part of good OO design) and knowing what the memory alignments are. Let's not take it to extreme. But to me, putting too much thinking into very low level things distracts you from bigger tasks.
So what are you suggesting?
You seem to agree that certain classes need to be memory optimized. How you can you optimize, if you don't know how much memory is consumed? Just guessing?
Just compare the memory usage of JHAT against the Eclipse Memory Analyzer and you will see what I mean.
JHAT uses a "naive" OO Model whereas Eclipse MAT is very much optimized and uses primitives for the most frequently used data.
Comments
zone_reader replied ago:
There are hundreds of murky little things in JVM knowledge of which does not make one a better programmer. You do not design your classes based memory alignments but rather on OO principles, maintainability patterns, and so on. If you do your design based on memory alignments your code becomes garbage and should be collected.
eelmore replied ago:
This statement explains one of the biggest reasons java apps are slow and require enormous amounts of memory.
mt79448 replied ago:
In respect of memory alignment, he is correct --- in Java you should pay very little attention to it. Unlike C/C++, a JVM is permitted to arrange fields in an object in whatever order is best. This can eliminate much of the alignment padding that would occur in a carelessly arranged C/C++ structure. Even better, the fields of a subclass can be located in spare space within its parent (though implementation of this optimization is quite recent).
While Java does have higher minimum memory requirements than C (perhaps 8MB for non gui, 16MB for GUI apps), memory use beyond this is largely the choice of the developer and not really a function of the language. For example, many IDEs of recent design are quite large, regardless of the implementation language.
kohlerm replied ago:
It's not only about alignment, it's also about whether you should deep inheritance (which can increase memory) or whether you should char[] instead of String objects in certain place, trading better performance (less memory usage) for a less ideal OO design.
kohlerm replied ago:
I disagree.
It's not a good idea, to just design your classes on OO principles alone, because that would mean that you ignore resource consumption completely.
Why would you design a class, which you expect to be instantiated very often to wast memory, just because of OO principles?
You have to find the right balance.
zone_reader replied ago:
There is a difference between designing class that gets instantiated very often (which should be a part of good OO design) and knowing what the memory alignments are. Let's not take it to extreme. But to me, putting too much thinking into very low level things distracts you from bigger tasks.
bjupton replied ago:
which is why you shouldn't do anything in a comparatively lower level language like Java and jump right to Python or Ruby.
kohlerm replied ago:
Python and Ruby can run on the JVM so what is point?
Support for optimizing Python or Ruby programs on their native implementation is very poor.
kohlerm replied ago:
So what are you suggesting?
You seem to agree that certain classes need to be memory optimized. How you can you optimize, if you don't know how much memory is consumed? Just guessing?
Just compare the memory usage of JHAT against the Eclipse Memory Analyzer and you will see what I mean.
JHAT uses a "naive" OO Model whereas Eclipse MAT is very much optimized and uses primitives for the most frequently used data.
Voters For This Link (12)
Voters Against This Link (9)