com.jcraft.jsch.Channel using lot of memory
Within our product we saw that com.jcraft.jsch.Channel uses ‘1 GB' memory in many 600KB buffers in its com.jcraft.jsch.Channel by >2000 ChannelSftp instances (all different connections and needed).
Further investigation turned out that for each PipedInputStream a buffer size is reserved (512 KB) at the start of the ChannelSftp class. This buffer size is calculated based on the fixed RequestQueue size (16) and multiplied with the Remote maximum packet size (32 * 1024). The Request Queue size 16 makes only sense for large files (several MBs up to hundreds of MBs) and connections with latency. for small files (KBs) a Request Queue size of 1 or 2 is sufficient occupying only 32KB or 64KB for each PipedInputStream.
Because we reuse sessions the Channel(Sftp) instances are never closed/disconnected, hence the reserved PipedInputStream memory is never released/freed. This caused the constant high memory usage by the Channel class.
Via method setBulkRequests(int size), the number of Request Queues can be influenced, however upfront we don't know the file size nor the latency impact. Reducing the Request Queue can therefore have a severe performance impact.
Question: Is it possible that the initialization/reservation of the PipedInputStream buffer size is done at the moment the additional buffer space is needed, hence lazy/dynamical? Also can it be released when it's not used anymore? This instead of reserving the fixed 512KB of which most of the time only 32KB is used.