2

Question: What is the performance cost of wrapping an object inside of another object? Both in RAM and CPU usage increase from calling the object directly.

Extra Details: I'm working on developing a framework that will wrapper code to isolate plugins from the core implementation of a program. The question keeps popping up in my head "What is the performance cost". I know it will be more than direct access to the code being wrapped. However, I can not seem to find any solid sources or data online to show exactly what results to expect. Which is an issue as I'm developing this to replace the implementation for roughly 40 projects. So when asked what level of overhead is expected in the update I need to have some data.

Also, in case anyone is unsure what I mean by wrapped code. It is the process in which an object is created using an interface that sends all method calls to an object stored in the object. This is done to hide the implementation of an object when the original object can not be modified or changed to include the interface. Below is an example of what I'm talking about.

public class WrapperObject implements WrapperInterface
{
    private SomeObjectClass theObject;

    public WrapperObject(SomeObjectClass theObject)
    {
        this.theObject = theObject;
    }

    public void method1()
    {
        theObject.method1();
    }

    public void method2()
    {
        theObject.method2("someData", 1);
    }
}

As well I do know about JIT in java and other optimization technics the JVM does. I'm asking what I can expect from the overhead of using a system like this for a large project. As I will be wrapping or converting just about everything before it gets to the plugin code. This includes methods that may be called a few 1000 times a second as well as methods only called once.

I will also be building prototypes in the next few days to test performance as well. I'm just looking for some design input to better understand what to expect. Rather than looking blindly at my code making guesses that waste time.

DarkGuardsman
  • 126
  • 4
  • 15
  • 1
    Measure it, don't make assumptions. The performance cost could be 0 if the JIT decides to inline or to call the wrapped object directly. – john16384 Feb 22 '17 at 12:29
  • As I noted I know about JIT and I am not making assumptions. I do plan to do testing on prototypes. What I'm looking for is some knowledge to help explain what I'm seeing. This way I do not make assumptions and guess at what is happening. – DarkGuardsman Feb 22 '17 at 13:34
  • If the performance cost of wrapping an object is a concern, you might as well give up on object-oriented programming. This is the absolute last thing you should be worrying about in Java. – jaco0646 Feb 22 '17 at 14:09
  • It is not a concern, I'm just looking to answer a question that has been in the back of my head for a while. I keep ignoring it as I know its not that major of a problem. However, understanding the impact may help me improve my choices in picking design patterns to use. – DarkGuardsman Feb 22 '17 at 14:33

1 Answers1

2

It is hard to estimate performance/CPU consumption because it really depend on what are you going to do with that objects and how you're going to process them. But you can measure RAM consumption relatively well.

Without going into details you can measure size of an object as sum of

  • size of headers (16 bytes for 64 bit JDK)
  • size of primitive members (find sizes in the documentation)
  • size of reference members (8 bytes for 64 bit JDK)
  • offset size (few bytes for equalization)

Your particular object will have headers, one reference plus offset.

Note this is approximation the exact size depend on a lot of other factors, but it will help you imagine how much memory will be consumed.

For more details on sizes of different types take a look on this question

Community
  • 1
  • 1
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
  • Thank you that will help out rather well. You care to at least take a guess on the CPU part? – DarkGuardsman Feb 22 '17 at 13:33
  • It is very hard even to make a guess because there are may factors that influence on performance (from use case of the application to garbage collector strategies). The only thing i can suggest is to read all about java memory model and analyze your use cases. You can start with [this](https://dzone.com/articles/java-performance-tuning) – Sasha Shpota Feb 22 '17 at 14:08
  • Thank you, I'll read that here in a bit and see if it helps :) – DarkGuardsman Feb 22 '17 at 14:34