Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-346

huge memory impact on receiving big packets (about 1MByte) due to CumulativeProtocolDecoder.storeRemainingInSession behaviour

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 1.0.1
    • 1.0.2
    • Filter
    • None
    • Windows XP Sp2
      Java 1.5.0_10
      Client / Server VM

    Description

      When receiving big TCP packets (about 1Mbyte or more) the current behavior of CumulativeProtocolDecoder.storeRemainingInSession implies that the session buffer is duplicated (and data copied) even if there is room for receiving another chunk of data from the socket (i.e. buf.limit() < buf.capacity()). In this situation there are spikes of memory consumption due to the fact that 1Mbyte / 8k (== session.getReadBufferSize()) = 128 buffers of increasing size are required for reading the entire packet.

      The attached patch prevents this, by explicitly checking if there is at least session.getReadBufferSize() space in the buffer, before requesting the allocation of a new buffer.
      The method session.getReadBufferSize() has been added to the IOSession Interface.

      private void storeRemainingInSession(ByteBuffer buf, IoSession session)
      {
      if (buf.limit() + session.getReadBufferSize() < buf.capacity()) // reuse the buffer
      {
      buf.position(buf.limit());
      buf.limit(buf.capacity());

      ByteBuffer sessBuff = (ByteBuffer) session.getAttribute(BUFFER);
      if (sessBuff != buf) // this should happens only the first time when sessBuff is null

      { session.setAttribute( BUFFER, buf ); buf.acquire(); }

      }
      else // old behavior

      { ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() ); remainingBuf.setAutoExpand( true ); remainingBuf.put( buf ); session.setAttribute( BUFFER, remainingBuf ); }

      }

      Attachments

        Activity

          People

            trustin Trustin Lee
            andrea.sillari andrea sillari
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: