Article View: borland.public.jbuilder.java.api
Article #987Re: OutOfMemoryError
From: Gillmer J. Derge [TeamB]
Date: Tue, 02 Jul 2002 14:40
Date: Tue, 02 Jul 2002 14:40
60 lines
3290 bytes
3290 bytes
"Alan Shiers" <ashiers@hfx.eastlink.ca> wrote in message
news:3D21C1CA.F2D953E2@hfx.eastlink.ca...
> 1) On the client-side app I read the size of the file and send out the
> file size value to the server app via DataOutputStream. I then fill a
> byte array with the entire file on the client-side app.
>
> 2) Then I use a for loop to send the contents of the file, one byte at a
> time, out through the DataOutputStream.
I would change these so the read and write happen simultaneously.
Something like this:
int c;
while ((c = in.read()) != -1)
out.write((byte)c);
Now there's no buffer, and your process only needs to remember one byte at
a time. Then I'd change this again to do the same thing but in blocks of N
(ex. N=64 or N=128) bytes instead of one at a time.
byte[] buffer = new byte[128];
int n;
while ((n = in.read(buffer)) >= 0)
out.write(buffer, n);
> 3) On the server-side app I receive the file size value via
> DataInputStream and dimension a byte array to accomodate the incoming
> file.
>
> 4) I use a for loop to retieve the contents of the file via
> DataInputStream one byte at a time on the server-side app.
Do the same thing on the server. Read small chunks and write them out to
the destination file.
> Maybe I should be using a Vector instead of dimensioning a byte array
> everytime?
That would probably be worse. Vectors can only contain objects, not
rudimentary data types, so what is now a byte or int would need to be a
Byte or Integer instead. Objects take up more memory, so you'd actually
run out of memory sooner this way.
> Taking my four step algorithm above, how would you change it to safely
> download large files?
See above.
> Should the changes I make be only on the server side or should I
> implement a change at both ends?
Both sides. The sever might be a more powerful machine with more memory,
but the fundamental problem will still exist on both sides. No matter how
much memory the machine has, there will always be some giant file size that
will cause you to run out. If you process more manageable chunks of data,
then you can essentially eliminate this problem (as long as you have at
least 256 bytes of memory :-).
--
Gillmer J. Derge [TeamB]
Technical Details
Message-ID:
<3d21bb44_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-06!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!proxad.net!gatel-ffm!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,28eab50e784856ac X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-07-02 07:43:03 PST From: "Gillmer J. Derge [TeamB]" <spam@gillmerderge.com> Newsgroups: borland.public.jbuilder.java.api References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com> <3D21C1CA.F2D953E2@hfx.eastlink.ca> Subject: Re: OutOfMemoryError Date: Tue, 2 Jul 2002 10:40:24 -0400 Lines: 63 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 24.58.130.24 Message-ID: <3d21bb44_1@dnews> X-Trace: dnews 1025620804 24.58.130.24 (2 Jul 2002 07:40:04 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-06!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!proxad.net!gatel-ffm!newsfeed01.sul.t-online.de!t-online.de!dnews