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

java.nio.channels.Selector causes unnecessary garbage collection

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 6u17
    • core-libs
    • x86
    • linux

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0_17"
      Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
      Java HotSpot(TM) Server VM (build 14.3-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC 2009 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Every select in java.nio.Selector, creates an Iterator. This is iterator is subsequently discarded. When continuously listening for data, Iterators are created and discarded unnecessarily, leading to garbage collection. There is no way to use the java.nio.Selector without avoiding garbage collection.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Any code that uses selector with multiple network connections. I've included a sample source code from the internet.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no garbage collection
      ACTUAL -
      garbage collection occurs frequently

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      <pre>
      // Wait for events while (true) {
         try {
            // Wait for an event
            selector.select();
         } catch (IOException e) {
            // Handle error with selector
            break;
         }

         // Get list of selection keys with pending events
         Iterator it = selector.selectedKeys().iterator();
         // Process each key at a time
         while (it.hasNext()) {
            // Get the selection key
            SelectionKey selKey = (SelectionKey)it.next();
             try {
                processSelectionKey(selKey);
             } catch (IOException e) {
                // Handle error with channel and unregister
               selKey.cancel();
             }
         }
      }
      </pre>
      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: