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

getCurrentThreadAllocatedBytes default implementation s/b getThreadAllocatedBytes

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • openjdk8u282
    • core-svc
    • None
    • behavioral
    • minimal
    • Behavior will match the JDK implementation of c.s.m.ThreadMXBean, which currently throws UOE. If that changes, or if a different class implements c.s.m.ThreadMXBean and overrides getCurrentThreadAllocatedBytes' behavior, behavior may change.
    • Java API
    • JDK

      This is an 8u backport of "JDK-8232072: getCurrentThreadAllocatedBytes default implementation s/b getThreadAllocatedBytes".

      Per the Description of "JDK-8251498: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument", the JMM version is not incremented by this CSR. Rather, this CSR updates the existing JMM version in a backwards and binary compatible way by using currently unused slots in the JMM structure. The @since javadoc tag is set to the target version of 8u282.

      This CSR is a follow-on to "JDK-8247812: [REDO] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread", and should be approved at the same time because it fixes a bug in that CSR.

      Summary

      To match its abstract specification, change the default implementation of com.sun.management.ThreadMXBean.getCurrentThreadAllocatedBytes to return the value of getThreadAllocatedBytes(Thread.currentThread().getId()).

      Problem

      The existing default implementation of com.sun.management.ThreadMXBean.getCurrentThreadAllocatedBytes is to throw UnsupportedOperationException, which is permitted but doesn't match the abstract specification of the method.

      Solution

      Change the default implementation of com.sun.management.ThreadMXBean.getCurrentThreadAllocatedBytes to return the value of getThreadAllocatedBytes(Thread.currentThread().getId()).

      Remove the @implSpec portion of the Javadoc that described the throwing of the exception.

      Specification

      /**
       * Returns an approximation of the total amount of memory, in bytes,
       * allocated in heap memory for the current thread.
       * The returned value is an approximation because some Java virtual machine
       * implementations may use object allocation mechanisms that result in a
       * delay between the time an object is allocated and the time its size is
       * recorded.
       *
       * <p>
       * This is a convenience method for local management use and is
       * equivalent to calling:
       * <blockquote><pre>
       *   {@link #getThreadAllocatedBytes getThreadAllocatedBytes}(Thread.currentThread().getId());
       * </pre></blockquote>
       *
      -* @implSpec The default implementation throws
      -* {@code UnsupportedOperationException}.
      -*
       * @return an approximation of the total memory allocated, in bytes, in
       * heap memory for the current thread
       * if thread memory allocation measurement is enabled;
       * {@code -1} otherwise.
       *
       * @throws java.lang.UnsupportedOperationException if the Java virtual
       *         machine implementation does not support thread memory allocation
       *         measurement.
       *
       * @see #isThreadAllocatedMemorySupported
       * @see #isThreadAllocatedMemoryEnabled
       * @see #setThreadAllocatedMemoryEnabled
       * @since 8u282
       */
      public default long getCurrentThreadAllocatedBytes() {
          return getThreadAllocatedBytes(Thread.currentThread().getId());
      }

            phh Paul Hohensee
            phh Paul Hohensee
            Volker Simonis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: