2

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.

Robert Harvey
201k55 gold badges470 silver badges682 bronze badges
asked Jun 11, 2019 at 16:04
3
  • 8
    What actual problem are you trying to solve here? Commented Jun 11, 2019 at 16:23
  • 3
    In 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. Commented Jun 11, 2019 at 16:38
  • 1
    Possible duplicate of Is micro-optimisation important when coding? Commented Jun 12, 2019 at 5:15

2 Answers 2

13

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.

answered Jun 11, 2019 at 17:04
3
  • 1
    In 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. Commented 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. Commented 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. Commented Jun 16, 2019 at 19:39
1

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.

answered Jun 12, 2019 at 6:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.