Does it optimize a program in java to declare methods private wherever they can be private or it doesn't change a thing and it's just a question of encapsulation? I was asking myself if declaring a method public was heavier than a declaring it private in terms of memory consumption.
-
8What actual problem are you trying to solve here?Philip Kendall– Philip Kendall2019年06月11日 16:23:17 +00:00Commented Jun 11, 2019 at 16:23
-
3In Java your method declarations only use a tiny bit of memory in the meta (or permanent) space. Even if this was the case, it's irrelevant unless you have some insane number of class definitions.JimmyJames– JimmyJames2019年06月11日 16:38:08 +00:00Commented Jun 11, 2019 at 16:38
-
1Possible duplicate of Is micro-optimisation important when coding?Doc Brown– Doc Brown2019年06月12日 05:15:02 +00:00Commented Jun 12, 2019 at 5:15
2 Answers 2
private
methods can never be overridden, whereas protected
and public
methods can be overridden.
As a consequence of this, the underlying runtime knows that for private
methods:
- There is no need to place them in the method table
- It can invoke them using non-virtual dispatching
By contrast, protected
and public
methods are most likely placed into the method table, and, analysis would take place to determine whether to invoke using virtual dispatch through the method table or not. (To err on the side of correctness, virtual dispatch would be used. Due to dynamic class loading the method may potentially be overridden sometime in the future, which might require adjustment to optimistic choices.)
The above does not take into account final
classes: methods in final classes share the above discussed qualities among all methods private
, protected
, and public
.
But worrying about this is micro optimization. It is very unlikely that virtual dispatch will have a meaningful effect on the performance or memory consumption of your application.
-
1In a hot loop, it can have a very noticeable impact on the performance. This is the difference between monomorphic and megamorphic calls that prevent JIT from doing method inlining and other optimizations.Juraj Martinka– Juraj Martinka2019年06月14日 05:30:09 +00:00Commented Jun 14, 2019 at 5:30
-
@JurajMartinka: The JVM supports speculative inlining. That is, the mere use of a protected or public modifier does not make a call site megamorphic, or prevent inlining or other optimizations.meriton– meriton2019年06月15日 18:35:09 +00:00Commented Jun 15, 2019 at 18:35
-
@meriton that is correct. What makes a callsite megamorphic isn't actually a number of different "subtypes" present int the program code or dependencies but what JIT observes at runtime. I was commenting on "It is very unlikely that virtual dispatch will have a meaningful effect on the performance or memory consumption of your application." which is imho false.Juraj Martinka– Juraj Martinka2019年06月16日 19:39:31 +00:00Commented Jun 16, 2019 at 19:39
Access modifiers like private
and public
have no effect on the resource requirements of a Java program. They exist largely as documentation and aids to understanding for human readers and writers of the code. Even when information about the status of a field or method is retained at runtime, it's likely to be stored in a bitfield-like structure, so either value takes the same amount of memory.
In short, making things private
does not optimize computer memory (RAM) in any way. It can, however, be better for human memory, because an API with fewer visible members is easier to understand and easier to remember than an API with everything public
.