CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    25

    Java.lang.OutOfMemoryError

    "Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space"

    Ok, so I have a problem that is giving me this error and I am hoping that there might be someone who could help me out.

    Code:
        private String CalcFileSize(){
                System.out.println("Calculating Size of File");
                File _tempFile = new File (fileName.getText());
                System.out.print(_tempFile.length());
                byte [] _fileSizeGetter = new byte [(int)_tempFile.length()];
                _fileSizeToSend = _fileSizeGetter.length;
                String _sendFileSize = Integer.toString(_fileSizeToSend);
                System.out.println("File Size Calculated");
                return _sendFileSize;
    
        }
    Ok, this function basically creates a File called _tempFile and loads something into it (for example, if fileName.getText() == "source.pdf" then it will grab that file and throw it into _tempFile). The problem occurs when I use a large file. Particularly something like a 200meg file. Anyone have a suggestion on a possible way around this?

  2. #2
    Join Date
    Nov 2006
    Posts
    25

    Re: Java.lang.OutOfMemoryError

    Seems that it can handle up to about 50 megs and then that is that.

  3. #3
    Join Date
    Oct 2008
    Posts
    77

    Re: Java.lang.OutOfMemoryError

    you need to specify larger heap
    -Xms<size> set initial Java heap size
    -Xmx<size> set maximum Java heap size
    http://blogs.sun.com/watt/resource/j...ions-list.html

  4. #4
    Join Date
    Apr 2007
    Posts
    442

    Re: Java.lang.OutOfMemoryError

    Much better idea is not to read the whole file at once, read a bit and increment a counter, keep on going untill end of stream. If you simply keep maxing your heap size, that is a row never ending. There are always larger files.

  5. #5
    Join Date
    Feb 2008
    Posts
    966

    Re: Java.lang.OutOfMemoryError

    I do not see anything in your code that indicates what kind of input/output stream you are using. I routinely read and write files over 500MB here without having any OutOfMemory errors. Are you using BufferedInputStream?

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured