Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8177107

Reduce memory footprint of java.lang.reflect.Constructor/Method

    XMLWordPrintable

Details

    Backports

      Description

        A DESCRIPTION OF THE REQUEST :
        Constructor- and Method instances from the JDK reflection API contain the associated parameter- and exception types as Class<?> member field arrays. However, if no parameters or exceptions are associated with the Constructor/Method instance, empty arrays are created and initialized to the instance.

        In my application the amount of reachable/long-lived zero-sized arrays originating from these reflection classes amount to tens of thousands. The proposal is to optimize the memory retention for these classes by using an array constant. For instance, the below package-private constant can be defined in AccessibleObject.java:

               static final Class<?>[] NO_CLASSES = new Class<?>[0];

        In the constructors of Method/Constructor classes the following code could be added:

                this.parameterTypes = parameterTypes.length == 0 ? NO_CLASSES : parameterTypes;
                this.exceptionTypes = checkedExceptions.length == 0 ? NO_CLASSES : checkedExceptions;

        In case Constructor- and Method instances are long-lived on the heap (which is probably often the case), this minor change would reduce the number of empty arrays lurking around.

        This change is also safe from an API perspective since the array is always cloned before being returned to the outside world. Callers of e.g. ::getParameterTypes will always receive a brand new array regardless of how the array is stored inside Constructor/Method classes.


        JUSTIFICATION :
        Reduce memory footprint for long-lived Constructor/Method objects


        Attachments

          Issue Links

            Activity

              People

                redestad Claes Redestad
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: