Memory consumption of anonymous classes vs. concrete subclass

Nir Lisker nlisker at gmail.com
Fri Mar 26 13:41:46 UTC 2021


Hi David,

Thanks for the reply.

What role does ThisClass play here?
>

It's the enclosing class of `SomeObjectProperty prop`. It should have been
a placeholder for some Object.

But the code is not effectively the same. The concrete class is a
> general class that can be instantiated multiple times for different
> beans and names - hence the fields are needed to store them. The
> anonymous class is for a singleton instance hard-wired to return one
> bean and one name. You could define a named class, specific to a given
> property, with no fields and which was also hard-wired the same way.
>

When I instantiate either of these classes, at the end of the day, I want
to return 2 constant values - the bean and the name. The concrete class is
immutable (no setters, and I should have put the fields as private final),
so once I create an instance of it, it is also hard-wired to return 2
specific values.
Another way to phrase my question is: is there a mechanism in which I can
create these anonymous classes by specifying the only important
information - the value of those 2 fields - without taking readability
punishment.
Maybe this thought experiment would help me: *what if* when I instantiate
the concrete class, it is converted in the background into the memory model
of the abstract class. What breaks in this case considering that they both
need to hold 2 constant values?

Thanks,
Nir

On Wed, Mar 24, 2021 at 6:00 AM David Holmes <david.holmes at oracle.com>
wrote:

> Hi Nir,
>
> On 24/03/2021 4:18 am, Nir Lisker wrote:
> > Hi,
> >
> > In JavaFX we have classes of the form
> >
> > abstract class SomeObjectProperty {
> >
> >      abstract String getName();
> >      abstract Object getBean();
> > }
> >
> > And a concrete
> >
> > class SimpleSomeObjectProperty extends SomeObjectProperty {
> >
> >      String name;
> >      Object bean;
> >
> >      SimpleSomeObjectProperty(String name, Object bean) {
> >          this.name = name;
> >          this.bean = bean;
> >      }
> >
> >      @Override
> >      abstract String getName() { return name; }
> >
> >      @Override
> >      abstract Object getBean() { return bean; }
> > }
> >
> > When we want to use a SomeObjectProperty, we usually use an
> > anonymous subclass:
> >
> > SomeObjectProperty prop = new  SomeObjectProperty() {
> >
> >      @Override
> >      public Object getBean() {
> >          return ThisClass.this;
>
> What role does ThisClass play here?
>
> >       }
> >
> >      @Override
> >      public String getName() {
> >          return "prop ";
> >       }
> > }
> >
> > The reason being memory consumption. The anonymous class takes less
> memory
> > because it holds less references. The amount varies depending on the
> number
> > of fields and their getters (in this case we had 2, but it could be
> more),
> > but we have done tests that show the memory footprint difference.
> However,
> > this is much less readable obviously and much longer.
> > Since JavaFX uses these properties extensively, the memory issue is
> > significant and the readability issue is prominent.
> >
> > I want the best of both worlds - more readable with less memory. I would
> > think that under the hood something could be done to optimize this code
> > since effectively it does the same. Is it possible to do something about
> > this?
>
> But the code is not effectively the same. The concrete class is a
> general class that can be instantiated multiple times for different
> beans and names - hence the fields are needed to store them. The
> anonymous class is for a singleton instance hard-wired to return one
> bean and one name. You could define a named class, specific to a given
> property, with no fields and which was also hard-wired the same way.
>
> Cheers,
> David
>
> > - Nir
> >
>


More information about the valhalla-dev mailing list