Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Applet out of memory exception

Posted on 2006-04-14
16
Medium Priority
?
188 Views
Last Modified: 2013-12-29
[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
0
Comment
Question by:InteractiveMind
  • 4
  • 4
  • 4
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16453332
You need to close files when you've finished with them
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16453336
... or indeed any stream 'handles'
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 1000 total points
ID: 16453379
>> 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16453420
>>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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16453439
Not sure but I've seen this work before in some other cases with ObjectOutputStream, etc
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16453954
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
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16454078
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
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16454092
(Hm. Perhaps it's a server property now allowing file uploads over a certain size?? ... I'll look into it..)
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16454308
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16455347
Try

out = new DataOutputStream( os ) ;

IOW, get rid of the BAOS
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16463717
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16760458
Split.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question