• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java.lang.OutOfMemoryError

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 4739
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic