Java heap space Exception and can't delete the file which recieved from server
<p>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 (b = new byte[1024 * 1024];)
Java Code:</p>

<pre><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();
        }
    }
}
</code></pre>

<p>}</p>

<p>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:</p>

<pre><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();
       }
</code></pre>

<p>}</p>

<p>in this case the Server will recieve the request from 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 <strong>"The action can't be completed because the file is open in Java(TM) platform SE Binary"</strong>. 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.
Thanks alot in advance of your reply!</p>
