-
Memory Leak Issue
Hi,
i am getting memory leak in this code
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
-
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.
-
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.
-
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.
-
Re: Memory Leak Issue
I am using groovy so no need for semicolon ";". :)
-
Re: Memory Leak Issue
I suggest you try a groovy forum then.
-
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.
-
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.