• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfEMemoryError  RSS feed

 
slimlys
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to read a file at any size. But when i try to read file with large
size , java.lang.OutOfMemoryError occures.
I tried to rectify it by finding out the runtimefree memory. but the
function java.lang.Runtime.getRuntime().feeMemory() returns incorrect value.
so i can't use that function for solving this error.

my code is :
public static byte[] fileRead(String fileName,int iOffset ) {
int length=0;
byte data[]=null;
try {
BufferedInputStream in = new BufferedInputStream(new
FileInputStream(fileName));
length=in.available();
data = new byte[length];
int size1=in.read(data,0,length);
} catch (IOException e) {
System.err.println("Exception in fileRead "+e.toString());
e.printStackTrace();
}
//System.out.println("return fileread "+data.length);
return(data);
}

How can i solve this problem. Can i get the error code fro this
OutOfMemoryError ?
pls reply me as soon as possible..
thanks in advance
Slimly.

------------------
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of reading the entire file in at once, create a loop and read the file in 1024 segments. You can still create a byte[] with the appropriagte size before you begin reading and add to the byte[] as you read 1024 blocks.
Hope that helps,
SAF
 
Jan Sauerwein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
... but the function java.lang.Runtime.getRuntime().feeMemory() returns incorrect value ...
No it doesn't.
It is only a value you don't need for that problem. It is the value of the free memory of the vm.
How you can solve your problem you have been told yet. So I have only one little hint for you.
If you want to know how you can manage hugh files look at the BSD-Code or better when it is open at the Code of MacOS X.
With a good structur and a good assessor you will have no problems to load, save and work with large files.
I hope it helps
j.a.n.s
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"slimlys" - Please read our JavaRanch policy on user names. We don't allow single-word user names here - please re-register with a name which includes a first and last name. Thanks.
Also, you may want to look at the RandomAccessFile class - it may already provide the functionality you need. Or it may not - it depends what exactly you want to do with the bytes after you read them, and how fast you need to be able to do it. For some applications I've found that RAF is too slow. But it may be worth a try at least...
[This message has been edited by Jim Yingst (edited July 08, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!