Delete memory allocated to a byte array?

Or have you just added a finaliser (probably unnecessary) that is preventing a significant amount of memory to be freed promptly.

For example in the update that shows what processData() does, you should really close the InputStreamReader – matt be Apr 27 at 18:14 @matt be In the updated code, it's not really a resource. – Tom Hawtin - tackline Apr 27 at 21:43.

If that is literally the only change you made, it is difficult to see how that would fix the problem. Whenever the Java Virtual Machine is running low on memory, it runs the garbage collector automatically before throwing an out of memory exception. There should be no need to do this yourself nor any value in doing so.

The only real solution to the problem you describe is to make sure that you clear out any references to objects that are no longer needed. Like if you say: byte ba=new bytebignumber; process(ba); and then you go on and do other things, ba is still sitting there hogging up memory. You want to either exit the function where it was defined, or set ba=null to lose the reference.

Then the gc can recycle the memory.

I tried ba = bull before break in my case statement and it did not help. System.gc() solved the problem for me. – user656189 Apr 27 at 16:57 Are you absolutely certain that you literally just added that one line and then the program worked?

Because according to the documentation, that's impossible. Frankly this sounds like saying that you typed x=2+2 and x ended up being 5. I guess it's not literally impossible, there could be a bug in Java or some subtlety in the specs that I'm missing.

But I've seen emphasized over and over JVM descriptions that the gc runs automatically when memory is low, and calling gc() is NEVER necessary. I would take your program as it is now, delete that gc call, CHANGE NOTHING ELSE, and see if it fails. – Jay Apr 28 at 19:41.

If you already know the structure of your content, why create all the intermediate steps. You could just as well process(dIn) somehow. Also, just to confirm, is this running in a multi-threaded environment?

Right onw it is not running in multi-threaded env. I am reading my input in byte array as I recevied msg as a byte array where first byte is message type following with 4 bytes of msg length and followed by msg content bytes. I read msg type and msg length and then pass byte array content which is an ascii string.

Now in process(), I rcv this byte array and do steps wrttn above in code for this func and add it to a stringbuilder obj. When these records(i.e. Multiple process() calls)reach a buffer size we want, we then use the stringbuilder output to dump this buffer to a file.

– user656189 Apr 27 at 18:12.

In which case you can declare your byte array outisde the while and reuse it at each iteration without allocating more memory: ... private static final int BUFFER_SIZE = 102400; //start with a buffer big enough to lower the chances of resizing it -- e.g. 100K ... msgContent = new byteBUFFER_SIZE; while (true) { msgType = dIn.readByte(); int msgIntType = msgType & 0xff; // get message length int msgIntLen = dIn.readInt(); if (msgIntLen! = 0) { if( msgIntLen > msgContent. Length ) //only resize when needed otherwise reuse msgContent = new bytemsgIntLen; switch(msgIntType) { case 4: //case MSG_DATA: // MSG_DATA recordCount++; processData(msgContent, msgIntLen); //need to change your function to also pass in the size of the message read!

If (recordCount == 2000) { sendACK(dOut, msgIntType); logger.Info("sent ACK for MSG_DATA"); recordCount = 0; } break; }.

I tried this solution also but we are running this JVM for different sizes of data stream where we don't know buffersize before hand. – user656189 Apr 27 at 16:58 you could adapt it to extend the buffer as needed so your buffer will extend only when the buffer size needs extending and otherwise just reuse the already allocated buffer. I'm about to edit my answer to show you how.

– Liv Apr 28 at 9:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions