Found that we are getting heap space problem from the JSch code. Here is the stack trace.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

        at com.jcraft.jsch.Buffer.getString(


        at com.jcraft.jsch.UserAuthPassword.start(

        at com.jcraft.jsch.Session.connect(

This Problem is happening rarely and also for some specific SSH Servers. I have looked at the Jsch source code and found that getString() is allocating a buffer of size found from getInt(). For these specific servers, getInt() value obtained has to be too large to cause it OutOfMemoryError. I dont have much resources/lab connection to further debug with the servers having this problem with a modified source code.

Is it known issue? Any suggestiions/hints on how to resolve/fix the issue?    Does upgrade to latest Jsch will fix it?


Here is the code that I believe is causing the issue:

public byte[] getString() {

int i=getInt();

byte[] foo=new byte[i];                     // This is the line causing the issue

getByte(foo, 0, i);

return foo;


public int getInt(){

int foo = getShort();

foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff);

return foo;


int getShort() {

int foo = getByte();

foo = ((foo<<8)&0xff00)|(getByte()&0xff);

return foo;


public int getByte() {

return (buffer[s++]&0xff);