NIO excessive Integer allocation

Jon V. sybersnake at
Mon Oct 31 20:20:00 UTC 2016

Hi everyone, #updateSelectedKeys() has a major object creation
problem.  It creates Integers in order to get the key from the map.

Changing Integer.valueOf() to new Integer() might allow escape-analysis to
remove the object.

Otherwise, fdToKey could be change to a Map<Number, SelectionKeyImpl> and a
new mutable Integer class could be created so only a single object would
need to be created per select() instead of every Key.

     * Update the keys whose fd's have been selected by the epoll.
     * Add the ready keys to the ready queue.
    private int updateSelectedKeys() {
        int entries = pollWrapper.updated;
        int numKeysUpdated = 0;
        for (int i=0; i<entries; i++) {
            int nextFD = pollWrapper.getDescriptor(i);
            SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
            // ski is null in the case of an interrupt
            if (ski != null) {
                int rOps = pollWrapper.getEventOps(i);
                if (selectedKeys.contains(ski)) {
                    if (, ski)) {
                } else {
          , ski);
                    if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
        return numKeysUpdated;

More information about the jdk8u-dev mailing list