944,221 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 21915
  • Java RSS
Aug 11th, 2005
0

Reading Large Files

Expand Post »
Hi everyone,

I am trying to to read and write large files about 600M.
The thing is when i use the normal FileOutputStream methods an exception gets thrown saying

Java Syntax (Toggle Plain Text)
  1. java.lang.OutofMemory

This is how i am trying to read the file

Java Syntax (Toggle Plain Text)
  1. FileOutputStream out = new FileOutputStream("C:/my_life_story.zip");
  2. InputStream in = //an input stream from a socket, etc
  3.  
  4. int len = 0;
  5. byte[] buffer1 = new byte[1024];
  6.  
  7. while ((len = in.read(buffer1)) > 0)
  8. {
  9. out.write(buffer1, 0, len);
  10. }
  11.  
  12. in.close();
  13. out.close();

My computer does have sufficient memory

I tried adding this in the while loop but it does not work and i got the same exception thrown by the JVM again

Java Syntax (Toggle Plain Text)
  1. System.gc();

My question is basically how do i increase the memory programatically
or is there away in which i can read the file without having the above exception thrown by the JVM?

Any help i greatly appreciated

Thank You

Yours Sincerely

Richard West
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Aug 11th, 2005
0

Re: Reading Large Files

Try flushing the buffer after every write.
The only thing I can think of that's eating up memory here is the outputbuffer. It may not be flushing automatically, causing the JVM to attempt to map the entire output into RAM instead of writing it directly to disk (which you seem to be attempting to do).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 12th, 2005
0

Re: Reading Large Files

Maybe you should try setting the maximum heap size for the JVM.
Java Syntax (Toggle Plain Text)
  1. java -Xmx256m MyClass
You can put higher values if needed.
For more help, www.NeedProgrammingHelp.com
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005
Aug 12th, 2005
0

Re: Reading Large Files

doesn't matter, having a 256MB heap won't help you contain a 600MB file in memory
You don't NEED to retain all that data in memory so the real solution is to not retain the data at all.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 12th, 2005
0

Re: Reading Large Files

Hi everyone,

Is this what you meant NPH

Java Syntax (Toggle Plain Text)
  1. java -Xmx650m MyProgram

Unfortunately i can't use the above code because the thing is the application is in a jar file on windows platform and users that use this application to run it usually double click on the jar file to run it. Is there a way to do what you suggested programatically maybe by the use of properties?

I think maybe jwenting maybe right that i have to flush and i will be trying it out.

On another question do any of you guys know how to increase the heap size programatically?

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: do while loop - not stopping!!
Next Thread in Java Forum Timeline: Can Anyone Help Me Please?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC