Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Sierra
  • 1 Post By viren2727in

Thread: Memory Leak Issue

  1. #1
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Memory Leak Issue

    Hi,

    i am getting memory leak in this code

    Java Code:
    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    buffer = new byte[5242880]; //1048576=1MB,5242880=5MB
    int xyza = 0
    while ((count = zis.read(buffer)) != -1) {
          baos.write(buffer, 0, count);
    }
    bytes = baos.toByteArray();
    baos.close()
    baos = null
    buffer = null
    if i comment " baos.write(buffer, 0, count); " this line , there is no more memory leak. anyone have idea how to resolve memory leak, i need file in byte array

    Thanks

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Memory Leak Issue

    Erm... how did you "detect" your memory leak? It is quite tricky with a virtual machine... ^^
    I can assure you that I do not see a leak here as such. Are you sure you clean up your OutputStream?
    Maybe it overflows?

    EDIT: Your code contains errors (line 1,3 is missing a ';' ) and is not executable. I can barely reproduce your problem.
    Last edited by Sierra; 01-09-2013 at 10:22 AM.
    I like likes!

  3. #3
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: Memory Leak Issue

    i use Java Visual VM, (Sampler Tab) to identify memoery leak, it shows bytes array consumes lots of memory and in last i get memory error.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Memory Leak Issue

    Can you try to reduce your buffer size. Maybe your output stream buffer is overflowing. Usually you do not need such a large buffer.
    Make sure your output stream is read, else you may buffer everything until you exceed the size of your JVM memory allocation.
    You may checkout the -Xmx switch for using more memory for your JVM.
    Last edited by Sierra; 01-09-2013 at 10:59 AM. Reason: bold
    I like likes!

  5. #5
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: Memory Leak Issue

    I am using groovy so no need for semicolon ";". :)

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Memory Leak Issue

    I suggest you try a groovy forum then.
    foulkelore likes this.
    I like likes!

  7. #7
    noctarius is offline Member
    Join Date
    Jun 2012
    Location
    Remscheid, Germany
    Posts
    61
    Rep Power
    0

    Default Re: Memory Leak Issue

    I would guess the problem is the internal bytearray inside of the ByteArrayOutputStream. This needs to get reallocated everytime it's to small for the additional data. Since you do not preallocate the BAOS it will grow very fast.

  8. #8
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: Memory Leak Issue

    Thanks to all for replay.

    we resolved the issue using visual VM. we are using hibernate, and hibernate sessions are not releasing memory. we cleared hibernate session and get solution.
    Fubarable likes this.

Similar Threads

  1. avoid memory leak
    By Sergey Livanov in forum Advanced Java
    Replies: 10
    Last Post: 12-20-2011, 04:10 PM
  2. Memory leak in swing
    By smeshram in forum AWT / Swing
    Replies: 7
    Last Post: 12-08-2011, 03:52 AM
  3. Replies: 1
    Last Post: 07-18-2011, 04:24 PM
  4. Help I have a memory leak...
    By cdman52 in forum Java Applets
    Replies: 10
    Last Post: 09-28-2009, 10:37 PM
  5. Memory Leak using a Swing Application Project
    By iimasd in forum AWT / Swing
    Replies: 0
    Last Post: 11-27-2007, 10:20 AM

Tags for this Thread

Posting Permissions

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