Results 1 to 12 of 12
- 12-26-2012, 06:05 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Java heap space problem, need help
I have written the code below, but after calling almost 150 times, it throws "Exception in thread "Thread-245" java.lang.OutOfMemoryError: Java heap space" the problem just ocure in line#19 (b = new byte[1024 * 1024];)
what is the exact problem causes this kind of problem, and also i declared the "byte b" as local variables, but the same problem happenedJava Code:class Client implements Runnable { private Socket socket; private BufferedInputStream bufin = null; private BufferedOutputStream bufout = null; String path; private byte[] b; Client(Socket socket, String path) { this.socket = socket; this.path = path; } @Override public void run() { try { bufin = new BufferedInputStream(socket.getInputStream()); bufout = new BufferedOutputStream(new FileOutputStream(path)); b = new byte[1024 * 1024]; int num = 0; while ((num = bufin.read(b)) != -1) bufout .write(b, 0, num); } catch (IOException e) { e.printStackTrace(); } finally { try { bufin.close(); bufout .close(); b = null; socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
what will be the reason?
Thanks alot in advance of your reply!
- 12-26-2012, 06:18 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Java heap space problem, need help
1. You should create your in- and output streams as well as your buffer only once at the beginning (e.g. in the constructor). Or (if that is really required) if the path changes.
2. You could increase the amount of memory for the JavaVM - which is not a solution of the root cause.
3. Call Thread.sleep(...) with a very low time after you did the job to prevent too fast allocation of objects.Last edited by Sierra; 12-26-2012 at 06:20 PM.
I like likes!.gif)
- 12-26-2012, 07:38 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Java heap space problem, need help
Post an SSCCE that demonstrates the problem. OutOfMemoryExceptions can be unpredictable during the runtime of a program, and the line they are thrown no necessarily indicative of where the problem truly lies - without knowing the context it makes it difficult to diagnose
- 12-26-2012, 10:54 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Java heap space problem, need help
Calling what for almost 150 times? If you have 150 clients running you have 150MB buffers allocated. Shrink the size of those data buffers; they are much too large now; 4096 bytes is more than enough.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-27-2012, 03:19 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: Java heap space problem, need help
Hi Friends!
Thanks from your reply, and i am sorry if i did not write the question clear in last post.
I try to describe the problem more clearly. it is like this:
I write i ServerSocket, when a client send the request the Sever then put the request in a new thread as below:
in this case the Server will recieve the request from the any client...Java Code:public void start() { boolean started = false; try { ServerSocket ss = new ServerSocket(8888); started = true; while (started) { String path = "C:/Pic/"+ new SimpleDateFormat("yy-MM-dd-HH_mm_ss_ms").format(new Date()) + ".jpg"; Socket s = ss.accept(); new Thread(new Client(s, path)).start(); } } catch (IOException e) { e.printStackTrace(); } }
and also the second problem which i think that problem will cause the Java heap space Exception, that is when
the Server recieve the picture, then the picture can not be deleted from the disk before you close the server, when i delete it,
it says "The action can't be completed because the file is open in Java(TM) platform SE Binary". Whereas i have already closed the input output in Client thread.
Now i can not discover the root of the problem, 1. why happen Heap Exception anr 2. why the file is not deletable from disk during server is running.
- 12-27-2012, 07:37 PM #6
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Java heap space problem, need help
Did you try any of my suggestions?
You should not have such a lot of connections and limit them.
If you really need indeed such a lot of connections you need to limit the buffer space as suggested above and you can increase the JavaVM memory by using the -Xmx startup parameter as I said above.
That you cannot delete the file is a common problem during multi threading - before deleting you should make sure that all is written to the file by flushing or closing the buffer. The deleting thread should always have a loop to wait until the file is accessible again. If 150 threads start locking files for reading - you will of course always have a thread that is accessing it, so you may not be able to delete it.
And again I say that you need to give the other threads time by using the Thread.sleep(...) method, even with very low values. Else your threads will deadlock your CPU.Last edited by Sierra; 12-27-2012 at 07:43 PM.
I like likes!.gif)
- 12-28-2012, 06:24 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: Java heap space problem, need help
Yes i tryed your sugestions, thanks for that!! but the problem was not solved, and also i searched about this, i got a little bit something more about this problem that is when i set the size of byte to the size of recived file like this "byte[] b = new byte[bufin.available()]" then the server only can recive one picture and the program going to not work and stoped and also the CPU usege goes to 100. now i think the problem will be with " byte[] b" to become cleared after each process finished. now how to do this and how close the connection between the Java(TM) platform and the file, which in such case the file also will be deletable and heaping problem also will solve. i did alot of experiment, but was no answered. at this point i will tell you that there is only one client that ervery 300ms send one picture to Server.
Thank You
- 12-28-2012, 06:42 AM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Java heap space problem, need help
Again, post an SSCCE. Often in the process of doing so the problem manifests itself - if not, it gives others the chance to reproduce the problem, or at the very least look at the problem in an appropriate context.
- 12-28-2012, 08:04 AM #9
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: Java heap space problem, need help
it is the entire code:
and it is the Client codeJava Code:import java.awt.*; import java.io.*; import java.net.*; import java.text.*; import java.util.Date; import javax.swing.*; public class ServerFrame { private ServerSocket ss = null; public ServerFrame() { start(); } public void start() { boolean started = false; try { ss = new ServerSocket(8888); started = true; while (started) { String path = "C:/Pic/" + new SimpleDateFormat("yy-MM-dd-HH_mm_ss_ms") .format(new Date()) + ".jpg"; Socket s = ss.accept(); new Thread(new Client(s, path)).start(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new ServerFrame(); } }
after recieving 150 picture happen heaping problem, and during the server executing, the recieved picture can't be modified (delete, move, copy, ...)Java Code:class Client implements Runnable { private Socket socket; String path; Client4(Socket socket, String path) { this.socket = socket; this.path = path; } @Override public void run() { BufferedInputStream bufin = null; BufferedOutputStream bufout = null; try { bufin = new BufferedInputStream(socket.getInputStream()); bufout = new BufferedOutputStream(new FileOutputStream(path)); byte[] b = new byte[1024 * 1024]; int num = 0; while ((num = bufin.read(b)) != -1) { bufout .write(b, 0, num); bufout .flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufin.close(); bufout .close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
i really don't know what is wrong with this code.
- 12-28-2012, 11:46 AM #10
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: Java heap space problem, need help
I found the problem that why happened Heap Exception, it is because the program doesn't reach to the "finally" block, which keep streams open for ever, and caused Heap Exception and also caused file can not delete or move. now i rewrite the code again as below, but i don't know why the program can not reach to the "finally" block, in such case what to do is better, in this point i need your help to show me. i solved the problem a little bit, but is this way correct? it is the question now.
in code above when close "bufin.close();" then the program will reach to "finally" block, if close the "bufout.close();" then again will not reach to "finally" block, but when i closed "bufin" in while loop , it will throw an Exception "java.io.IOException: Stream closed" but the file will recieved successfully.Java Code:public void run() { BufferedInputStream bufin = null; BufferedOutputStream bufout= null; try { bufin = new BufferedInputStream(socket.getInputStream()); fio = new BufferedOutputStream(new FileOutputStream(path)); byte[] b = new byte[1024 * 1024]; int num = 0; while ((num = bufin.read(b)) != -1) { bufout.write(b, 0, num); bufout.flush(); //bufout.close(); bufin.close(); //socket.close(); System.out.println("Reached..0"); } System.out.println("Reached..1"); // never will be print or never will reach here } catch (IOException e) { e.printStackTrace(); } finally { try { bufin.close(); bufout.close(); socket.close(); System.out.println("Reached..2"); } catch (IOException e) { e.printStackTrace(); } } }
now the problem is here that how to do that let the program reach to the "finally" block...Last edited by Bahramudin; 12-28-2012 at 11:49 AM.
- 12-28-2012, 03:53 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Java heap space problem, need help
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-29-2012, 02:05 PM #12
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Java heap space problem, need help
Hell, you cannot close your stream while you use it... you close it while still reading data in your while loop. That is not a good thing to do...
You also should consider using the available() method of your input buffer for doing those kind of operations. No close operation should occurr inside your loop but if you really need to do that - at the end.
Check if the items to be closed are valid and develop contingency ops if not. Probably your finally block is reached but fails again at the first command because you already did already close your buffer...I like likes!.gif)
Similar Threads
-
Java Heap Space Problem
By syrvn in forum New To JavaReplies: 11Last Post: 07-14-2011, 10:43 AM -
Java Heap Space Problem
By syrvn in forum New To JavaReplies: 1Last Post: 07-13-2011, 08:19 PM -
Problem JAVA HEAP SPACE !!
By someone in forum New To JavaReplies: 4Last Post: 05-01-2011, 07:12 PM -
large number of DB writes: java heap space problem in using transaction-commit
By narimani in forum JDBCReplies: 0Last Post: 06-28-2010, 12:30 PM -
Problem with Java Heap Space using Socket
By mvillara in forum NetworkingReplies: 3Last Post: 08-21-2009, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks