Link to home
Start Free TrialLog in
Avatar of mte01
mte01Flag for Lebanon

asked on

OutOfMemoryError

Hey experts,

A java.lang.OutOfMemory error does not happen usually on a PC application, but it's high probable to happen on a mobile application, and that's what happened with me when testing a J2ME application on a mobile phone (and specifically when the code was playing an audio message), and since this is an error and not an exception, handling it maybe a bit different. This is why I am asking this question: do I make a regular try block around the code that is playing this audio and make a catch block following it for an OutOfMemoryError and in it display the proper error message?? or it could be handled differently??
Avatar of sciuriware
sciuriware

You can't (catch an OutOfMemeory exception).

Please verify your way of dealing with objects; that's the cause.
;JOOP!
Avatar of mte01

ASKER

The code at the which this error is happening is as follows:

try
{
      InputStream bis = new ByteArrayInputStream(VoiceMessage.getBytes());

      Player player = Manager.createPlayer(bis, "audio/x-wav");
      player.prefetch();
      player.start();
                        
      display.setCurrent(Stop_Playing_Form);
}
catch(Exception ex)
{
      ex.printStackTrace();
}

The audio message to be played is sometimes turning out to be very large (exceeding the memory on the phone), and thus the error is happening....
Can't you check the length of VoiceMessage.getBytes(), then not play HUGE sounds (that would crash it)?

How are you getting the data for VoiceMessage?

There may be a way for you to chop it up, and only get a certain amount of bytes at a time?

Tim
Actually, it may be getBytes() that's causing it to crash?
Avatar of mte01

ASKER

>>Can't you check the length of VoiceMessage.getBytes(), then not play HUGE sounds (that would crash it)?

Yes, this is the most reasonable way, but it still depends on the phone/card memory of the user, i.e. it may be a medium-sized file, but the phone's empty memory is little to play this message.

>>How are you getting the data for VoiceMessage?

VoiceMessage is being copied from a string of data that is coming from a server via a socket

>>Actually, it may be getBytes() that's causing it to crash?

How can that be??
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mte01

ASKER

>>Can't you just point the ByteArrayInputStream at the socket?

OK...I'll be trying that and I'll inform you of the results....
Hope it works :-)

The sound might be a bit "choppy", so as I said, you may need to buffer the stream a bit...

But it should use less memory than reading it all into a bye array, then playing that array :-)

Tim
Did you get it working?

Hope so!

Good luck!

Ti
m

;-)
Avatar of mte01

ASKER

>>TimYates

I guess I'll be using buffering, I asked a question regarding it:
https://www.experts-exchange.com/questions/21532858/Buffering-in-an-input-stream.html