Results 1 to 4 of 4
  1. #1
    redpois0n is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Clear bytearrayoutputstream, need help

    Hello. I am new here, I am pretty big on other forums, but this is new.

    I have worked on java for around 1 year.

    Now here is my error: I transfer file through socket, by first writing it to a bytearrayoutputstream.

    Java Code:
    byte[] buffer = new byte[(int)file.length()];
    					        ByteArrayOutputStream ous = new ByteArrayOutputStream();
    					        InputStream ios = new FileInputStream(file);
    					        int read = 0;
    					        while ( (read = ios.read(buffer)) != -1 ) {
    					            ous.write(buffer, 0, read);
    					        }
    					        ous.flush();
    					        byte[] ar = ous.toByteArray();
    					        Connection.client.setSendBufferSize(ous.size());
    				            Connection.client.getOutputStream().write(ar, 0, ous.size());
    				            Connection.client.getOutputStream().flush();
    					        ous.reset();
    					        ous.close();
    					        ios.close();
    					        ios = null;
    					        ar = null;
    					        ous = null;
    					        System.gc();
    It writes OK and quick, but everything written to the bytearrayoutputstream stays there. Forever.

    If I write a 50 mb file, the memory goes up 50 mb, and in the end, it goes outofmemory.

    I will happily explain more if you dont understand.

    TL;DR

    Dispose of the object

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Clear bytearrayoutputstream, need help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    redpois0n is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Clear bytearrayoutputstream, need help

    nope.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Clear bytearrayoutputstream, need help

    everything written to the bytearrayoutputstream stays there. Forever.
    Can you demonstrate that with a small simple program that compiles and executes?

    Are you asking about data usage in an object
    or about the JVM's memory usage.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: 09-05-2011, 02:38 PM
  2. What's the best way to clear a Socket?
    By Joel in forum New To Java
    Replies: 2
    Last Post: 06-22-2011, 06:30 PM
  3. Clear JSP usebean
    By abbeywRCS in forum Advanced Java
    Replies: 1
    Last Post: 05-16-2011, 09:18 PM
  4. Display sound data value from ByteArrayOutputStream
    By cielle in forum Advanced Java
    Replies: 2
    Last Post: 12-14-2010, 09:47 AM
  5. ByteArrayInputStream and ByteArrayOutputStream
    By new_2_java in forum New To Java
    Replies: 6
    Last Post: 07-31-2008, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •