0

i use HttpUrlConnection and inputstream and bufferedInputStream and outputstream and BufferedOutputstream to download file from my wampserver , and it work correctly but when i trace may taskmanager and performence tab i see that my ram increase very much from 2.02 GB to about 3 GB :

this my code :


private void downloadFile() {
        try {
            this.response = this.con.getInputStream();
            this.bis = new BufferedInputStream(this.response, 1024 * 1024);
            this.responseContentSize = this.con.getContentLength();

            if (this.responseContentSize == (this.end_range - this.start_range) + 1) {

                byte buffer[] = new byte[MAX_BUFFER_SIZE];
                makeTmpFile();
                out = new FileOutputStream(this.tmpdir + this.tmpFilename);
                bos = new BufferedOutputStream(out, 1024 * 1024);
                while (true) {
                    int read = bis.read(buffer, 0, this.MAX_BUFFER_SIZE);
                    if (read == -1)
                        break;

                    bos.write(buffer, 0, read);
                    downloadedBytes += read;

                }

                if (bos != null)
                    bos.close();
                if (out != null)
                    out.close();
                if (fpointer != null)
                    fpointer.close();
                if (bis != null)
                    bis.close();
                if (response != null)
                    response.close();
                2);//finish
            }
        } catch (IOException e) {
            sharedDownloadStatus.setCell(this.threadIndex, STATUS, 0);//error
            log.setLog("func : downloadFile =>\n couldnt download file\n" + e.getMessage());
        }

}

i created this func in class that implement Runnable to begin its task in another thread

wineo
  • 1
  • 1
  • 1
    What is the value of `MAX_BUFFER_SIZE`? – Joe Bauer Jun 05 '14 at 18:11
  • Did you check how many times this method is getting invoked ? Also it'd help if you can take the memory dump and analyze it using Eclipse MAT tool. – saratbeesa Jun 05 '14 at 18:22
  • this method invoked 8times in 8 thread that work simultaneously and i will check it by MAT thanks . i created download manager like IDM and work good but consume lot of ram and get my win7 slow. – wineo Jun 05 '14 at 18:30
  • Can you directly write to FileOutputStream like in this question? http://stackoverflow.com/questions/33231511/how-to-download-a-file-and-get-the-path-location-locally/33231728#33231728 – Ravindra babu Oct 21 '15 at 07:07

0 Answers0