Applet out of memory exception

[I'm currently only executing my applet via Eclipse - but will be shoving it into a webpage soon]


My applet reads a file into chunks (where 'is' is the FileInputStream):


byte [] data = new byte [1024] ;

int r = 0 ;
while ( ( r = is.read( data, 0, data.length ) ) > 0 )
{
      out.write( data, 0, r ) ;
}


It then buffers the chunks to a server/PHP script, which then outputs the data to a file on the server.


For the larger files, I get an OutOfMemory exception when I try and upload them.

Firstly, where is the mem leak in my above technique??

Secondly, how can I extend the amount of available mem to my signed applet?

Thanks
LVL 25
InteractiveMindAsked:
Who is Participating?
 
CEHJCommented:
You need to close files when you've finished with them
0
 
CEHJCommented:
... or indeed any stream 'handles'
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> out.write( data, 0, r ) ;

do an out.flush () after that in the loop. And make sure the streams are closed in finally blocks.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
CEHJCommented:
>>do an out.flush () after that in the loop

You shouldn't need to in fact. If the buffer is full it will auto-flush.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Not sure but I've seen this work before in some other cases with ObjectOutputStream, etc
0
 
InteractiveMindAuthor Commented:
Okay,

here's my write method:


      public void writeFile( String mimeType, String fileName, InputStream is, long length,
                  DnDComponent dndC ) throws Exception
      {
            try
            {
                  if ( is == null ) { throw new IllegalArgumentException( "Input stream cannot be null." ) ; }
                  if ( fileName == null || fileName.length() == 0 ) { throw new IllegalArgumentException(
                              "File name cannot be null or empty." ) ; }
                  
                  out.writeBytes( PREFIX ) ;
                  out.writeBytes( boundary ) ;
                  out.writeBytes( NEWLINE ) ;
                  // write content header
                  out.writeBytes( "Content-Disposition: form-data; name=\"userfile\"; filename=\""
                              + fileName + "\"" ) ;
                  out.writeBytes( NEWLINE ) ;
                  if ( mimeType != null )
                  {
                        out.writeBytes( "Content-Type: " + mimeType ) ;
                        out.writeBytes( NEWLINE ) ;
                  }
                  out.writeBytes( NEWLINE ) ;
                  // write content
                  byte [] data = new byte [1024] ;
                  int r = 0 ;
                  int pr = 0 ;
                  while ( ( r = is.read( data, 0, data.length ) ) != -1 )
                  {
                        out.write( data, 0, r ) ;
                        pr += r ;
                        
                        this.r = pr / (double ) length ;
                        
                        dndC.updateUpload( this.r ) ;
                  }
                  
                  try
                  {
                        is.close() ;
                  } catch ( Exception e )
                  {}
                  out.writeBytes( NEWLINE ) ;
                  out.flush() ;
            } catch (Exception e)
            {e.printStackTrace();}
      }



Now, the OutOfMemoryError occurs after uploading the first ~30MB of a file - if that's of any use, to know :)

Also, I'm having another problem with this same method; even though the mime type is "application/octet-stream", which should apparently support any file type... this method appears to be uploading any file with no problems (excluding the memory problems); but files of certain file types are not appearing on the server... the file types that I've tried so far that have failed to appear on the server have all been media files - mp3, m4a, et al.  (However, MPEG is uploading with no problem).

Any guesses??

Thanks
0
 
InteractiveMindAuthor Commented:
After some more testing, it would seem that it's not certain file types that are failing to upload... but it's all down to the file sizes..

Files over around about 2-3 MB are not uploading properly (but I'm not getting any exception)..
0
 
InteractiveMindAuthor Commented:
(Hm. Perhaps it's a server property now allowing file uploads over a certain size?? ... I'll look into it..)
0
 
InteractiveMindAuthor Commented:
Yer; that prob turns out to be server error (I tried it out on another server)..

But still I get the mem exception for really large files...
0
 
CEHJCommented:
Try

out = new DataOutputStream( os ) ;

IOW, get rid of the BAOS
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Can you try to put an out.flush () after every out.writeBytes () that you do?

Anyway, more importantly can you try it after this, inside the loop:

>> out.write( data, 0, r ) ;
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Split.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.