×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    java.lang.OutOfMemoryError

    I tried to read test-wav.wav given in audiodemo project. But I got an exception java.lang.OutOfMemoryError at the line byte[] Data = baos.toByteArray();.

    How do I solve this?


    Code:
     ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ..
    ..
     byte[] Data = baos.toByteArray();
    
    java.lang.OutOfMemoryError
    	at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+309)
    	at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    	at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    	at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.OutOfMemoryError

    This happens, quite simply, because you have run out of memory. ByteArrayOutputStream.toByteArray() makes a copy of all the data in the object. Therefore, the total memory you will need is likely to be at least double the size of the "test-wav.wav" file.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    Please tell me where to make the change.
    Code:
    HttpConnection c = (HttpConnection) Connector.open("http://localhost/uploads/"+filename+".wav"); 		
    		byte receivedFile[]=null;
    		try{
    			DataInputStream response = new DataInputStream(c.openInputStream());
    			receivedFile = new byte[(int) (c.getLength()) ];
    			response.readFully(receivedFile);
    			response.close();
    			c.close();
    								 
    		}catch(java.lang.OutOfMemoryError e){
    				System.out.println("ere "+e);									
    		}
    I'm getting outofmemory exception. My file size is 49MB.
    Exception is thrown at this line
    receivedFile = new byte[(int) (c.getLength()) ];

  4. #4
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: java.lang.OutOfMemoryError

    49MB is a little too much for a mobile app I'm afraid...
    Take into account that before an app can write the data to your memory card (which can contain gigabytes of data) it will have to keep it local in it's local memory (called RAM) which is way way less... You might want to chop that data into pieces and download a piece at a time instead, and add that tou your local file...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  5. #5
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    What is the size of mobile RAM?
    How about 250KB file, is this short enough for mobile memory?

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.OutOfMemoryError

    That depends on the device. Tell us what device (or devices) you are working with (or look at their specifications). But I think no Nokia will allow you more that 16Mb (and remember you need at least twice the size of the file in free memory).

    Graham.

  7. #7
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    Now the file size is 164KB
    But I still get Out of Memory exception
    Code:
    HttpConnection c = (HttpConnection) Connector.open("http://localhost/uploads/"+filename+".wav"); 		
    byte receivedFile[]=null;
    try{
    	DataInputStream response = new DataInputStream(c.openInputStream());
    	receivedFile = new byte[(int) (c.getLength()) ];
    	response.readFully(receivedFile);
    	response.close();
    	c.close();
    								 
    }catch(java.lang.OutOfMemoryError e){
    	System.out.println("ere "+e);									
    }
    Exception is thrown at this line
    receivedFile = new byte[(int) (c.getLength()) ];

  8. #8
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: java.lang.OutOfMemoryError

    try to debug and see what value that c.getLength() has...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  9. #9
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    c.getlength() value 2033062

    original file size 1986KB.
    ...
    I have changed the code now
    Code:
              filecon = (FileConnection)
    	 Connector.open("file:///Card/Sounds/mynewfile.amr"); 
    	if(!filecon.exists()) {
    	         filecon.create();
    	}
    					   					
           	DataOutputStream output= filecon.openDataOutputStream();
          	int p=response.readInt();
    																			
    	while(p!=0){
    								
    	    output.writeByte((int)response.readByte());
    									
    	      p--;
    									
    	}
           output.close();
           filecon.close();
    I ran the file in mobile and got an EOFexception. If I change this line to
    p!=-1
    Still EOfexception remains.
    What should I do?

    The audio file runs properly.
    Last edited by csckid; 2010-09-14 at 19:49.

  10. #10
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.OutOfMemoryError

    Quote Originally Posted by csckid View Post
    Now the file size is 164KB
    Quote Originally Posted by csckid View Post
    original file size 1986KB
    So... how big is the file...?

  11. #11
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    Quote Originally Posted by Tiger79 View Post
    try to debug and see what value that c.getLength() has...
    c.getlength() value 2033062

    original file size 1986KB.

    Forget about the file 164KB file
    New Problem
    I have changed the code now
    Code:
     filecon = (FileConnection)
    	 Connector.open("file:///Card/Sounds/mynewfile.amr"); 
    	if(!filecon.exists()) {
    	         filecon.create();
    	}
    					   					
           	DataOutputStream output= filecon.openDataOutputStream();
          	int p=response.readInt();
    																			
    	while(p!=0){
    								
    	    output.writeByte((int)response.readByte());
    									
    	      p--;
    									
    	}
           output.close();
           filecon.close();
    I ran the file in mobile and got an EOFexception. If I change this line to
    p!=-1
    Still EOfexception remains.
    What should I do?

    The audio file runs properly.

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.OutOfMemoryError

    Do the first four bytes of the "response" stream contain the length?

  13. #13
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    Quote Originally Posted by grahamhughes View Post
    Do the first four bytes of the "response" stream contain the length?
    I don't know what you asking for.

    Forgot to mention to more lines.
    Code:
    	HttpConnection c = (HttpConnection) Connector.open("http://uploads/abc.amr"); 
    							
    	DataInputStream response = new DataInputStream(c.openInputStream());
         
            fileconn= (FileConnection)
    ..
    ..

  14. #14
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: java.lang.OutOfMemoryError

    I'm curious about this line of code:

    Quote Originally Posted by csckid View Post
    Code:
          	int p=response.readInt();
    It reads the first four bytes of the stream, and constructs an int from them. You then seem to be treating this as if it's the file length. How is the file length getting added to the start of the stream?

    Graham.

  15. #15
    Regular Contributor
    Join Date
    Jul 2009
    Posts
    51

    Re: java.lang.OutOfMemoryError

    THanks problem fixed
    instead of
    Code:
          	int p=response.readInt();
    I changed it to this
    Code:
          	int p=response.available();
    Last edited by csckid; 2010-09-15 at 14:01.

Similar Threads

  1. java.lang.OutOfMemoryError: connection
    By littleson in forum Mobile Java General
    Replies: 4
    Last Post: 2009-03-16, 11:11
  2. lcdui.Image.createImage -> java.lang.OutOfMemoryError: Java heap space
    By sbmcdeshan in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2009-02-23, 19:29
  3. java.lang.OutOfMemoryError
    By Falcco in forum Mobile Java Tools & SDKs
    Replies: 5
    Last Post: 2008-11-10, 12:47
  4. java.lang.OutOfMemoryError
    By Panta in forum Mobile Java General
    Replies: 10
    Last Post: 2008-06-20, 13:06
  5. java.lang.OutOfMemoryError
    By slatanx in forum Mobile Java General
    Replies: 2
    Last Post: 2004-08-24, 07:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×