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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You need to close files when you've finished with them

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
... or indeed any stream 'handles'
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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.
Mayank SAssociate Director - Product EngineeringCommented:
Not sure but I've seen this work before in some other cases with ObjectOutputStream, etc
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
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)..
InteractiveMindAuthor Commented:
(Hm. Perhaps it's a server property now allowing file uploads over a certain size?? ... I'll look into it..)
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...
CEHJCommented:
Try

out = new DataOutputStream( os ) ;

IOW, get rid of the BAOS
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 ) ;
Mayank SAssociate Director - Product EngineeringCommented:
Split.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.