• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfMemoryError

 
Shaun Mc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to read in an 18mb file into a byte array, although when i try to pass that byte array to a method by parameter i get:

java.lang.OutOfMemoryError



Please help with any suggestions/solutions

heres my code:

InputStream is = new FileInputStream(fileName);
long length = spssfile.length();
byte[] bytes = new byte[(int)length];
is.read(bytes, 0, bytes.length);
is.close();
System.out.println("Got this far");
XMLFile file = parseSavFromFile(bytes);
//file.retrieveSPSS();

}
catch (IOException e) {
//System.out.println("Error caught:" + e);
}
}



public static XMLFile parseSavFromFile(byte[] fileName) throws IOException{



byte[] contents = fileName;


try{



} catch (Exception e) {System.err.println(e.getMessage());
}

return parseSav(contents);



}


public static XMLFile parseSav(byte[] savData) {

XMLFile sav = new XMLSavFile(savData);

return sav;
}


}
[ September 28, 2005: Message edited by: Shaun Mc ]
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you should redesign your app.
Change the JVM size with a java.exe command option: -X... (read the doc) could give your program some breathing space.
 
Shaun Mc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norm,


Thankyou for your response, im afraid "redesign your app" doesn't give me much to go on.

The file is read correctly into the byte array and it HAS to be passed in whole to the method shown (which is when the error occurs), so I do not have much room for redesign.

When trying to increase JVM size i now get "Java.lang.NoClassDefFoundError" - nightmare, i know what this suggests, but i've modifying my classpath settings to point to the right directory etc, over and over.

Any ideas, anybody??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15711
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you add (for example) "-Xmx500m" to the command line, you suddenly get a NoClassDefFoundError, and if you remove it again, the error disappears?

If yes, you're doing something wrong on the command line...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem you will run into sooner or later is that is.read() doesn't do what you think it does. If you look at the InputStream API Doc, you'll see that it:
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

So you may not even have the data you think you do.
Norm is correct, reading in an 18Meg file and passing it around is a Bad Idea. You haven't included the details of much of your code (i.e. what is a XMLSavFile or the bulk of the parseSavFromFile method) or of the error for that matter. Exactly where does the out of memory error occur? Does it always occur in the same place?
As for NoClassDefFoundError, have a look at the JavaRanch Classpath FAQ. It isn't difficult. You just have to point out where the JVM can find the classes you are using.
 
Shaun Mc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Joe and Jesper for your responses, problem solved.

I was in fact not using the right path for my class, i soon realised that to solve the problem I had to include a reference to the package my class exists within (even while in the correct directory).

For the benefit of others:

java -Xmx256 org/mordicus/spssutils/readFile


Joe i will amend the code to ensure the file is read in completely,but im afraid I HAVE TO pass the complete file into a byte array which a prewritten java library uses for further processing, so my hands seem to be tied there.

Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!