title: Java OutOfMemory issue while transferring byte array
body: <p>I am writing remote desktop application. So I am transferring Images from one machine to another machine as byte array through socket. After receiving byte array I convert it into image and draw on a panel. Code looks approximately like below</p>
  
  <pre><code>imageBytes = //read from socket.
  InputStream in = new ByteArrayInputStream(imageBytes);
  BufferedImage bufferedImage = ImageIO.read(in);
  Image image = Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
  Image scaledImage = image.getScaledInstance(rmdPanel.getWidth(),rmdPanel.getHeight() ,Image.SCALE_FAST);
  Graphics graphics = rmdPanel.getGraphics();
  graphics.drawImage(scaledImage, 0, 0, rmdPanel.getWidth(),rmdPanel.getHeight(),rmdPanel);
  </code></pre>
  
  <p>I also store <em>imagebytes</em> till next image comes(for comparison). Now I am getting java out of memory exception in this code(while receiving byte array). I have heap size of 128 mb-512 mb. Image bytes sent are maximum 3mb.</p>