Results 1 to 12 of 12
  1. #1
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default 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];)

    Java 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 is the exact problem causes this kind of problem, and also i declared the "byte b" as local variables, but the same problem happened
    what will be the reason?
    Thanks alot in advance of your reply!

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

    Default 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!

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default 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:

    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();
                  }
        }
    in this case the Server will recieve the request from the any client...
    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.

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

    Default 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!

  7. #7
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default 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

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.

  9. #9
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Java heap space problem, need help

    it is the entire code:
    Java 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();
    	}
    
    }
    and it is the Client code
    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();
    			}
    		}
    	}
    
    }
    after recieving 150 picture happen heaping problem, and during the server executing, the recieved picture can't be modified (delete, move, copy, ...)
    i really don't know what is wrong with this code.

  10. #10
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default 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.

    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();
    			}
    		}
    	}
    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.
    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.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java heap space problem, need help

    Quote Originally Posted by Bahramudin View Post
    but i don't know why the program can not reach to the "finally" block
    If you're using a decent Java compiler that finally block is reached, whether or not an Exception was thrown; you can easily test it by sticking a System.out.println("in finally block ...") statement there and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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!

Similar Threads

  1. Java Heap Space Problem
    By syrvn in forum New To Java
    Replies: 11
    Last Post: 07-14-2011, 10:43 AM
  2. Java Heap Space Problem
    By syrvn in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 08:19 PM
  3. Problem JAVA HEAP SPACE !!
    By someone in forum New To Java
    Replies: 4
    Last Post: 05-01-2011, 07:12 PM
  4. Replies: 0
    Last Post: 06-28-2010, 12:30 PM
  5. Problem with Java Heap Space using Socket
    By mvillara in forum Networking
    Replies: 3
    Last Post: 08-21-2009, 04:45 AM

Posting Permissions

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