cancel
Showing results for 
Search instead for 
Did you mean: 

Memory issues while fetching content

Former Member
0 Kudos

HI all,

I am using SAP KM Apis. however i am facing some memory issues while using them.

I am using getContent() api on a resource. and then getInputStream on the Icontent object. I am using a ByteArrayOutputStream for writing the data. here is the code snippet:

byte[] buf= new byte[4096];

InputStream inputStream = content.getInputStream();

ByteArrayOutputStream os = new ByteArrayOutputStream(1024)

while ((count = inputStream.read(buf)) != -1)

{

os.write(buf, 0, count);

}

byte[] arr=os.toByteArray();

THis code consuimes a lot of memory . we need to optimize this. we also tried removing BYteArrayOutputSteam and directly read the bytes in chunk :

int n;

do

n = inputStream.read(buf, 0,(int)content.getContentLength());

while (n != -1);

however this approach has not helped us.

please suggest an approach where memory consumption is less and the entire content is fed.

thanks

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

we are returning the content from a par file deployed on sap server to an agent sitting on another machine. is there any other way we can return the data to the agent from a par file. Can we directly send the stream?

Former Member
0 Kudos

It probably helps reading larger chunks, to minimize the amount of times the underlying byte array needs to be resized,

I can't help with the other question as I don't know the API that is involved.

former_member188556
Active Contributor
0 Kudos

Hi

This code worked for me for reading from a KM res...

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
           }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
return sb.toString();

Regards

BP

Former Member
0 Kudos

Not sure what you want to achieve.

If you want to real the whole contents into a byte array, that will obviously consume at least the number of bytes the content has in size.

Please elaborate.

Former Member
0 Kudos

Thanks Julian for your answer.

we need the most optimized way to convert the inputstream returned by the content object into a byte array so that memory consumption is least.