Results 1 to 10 of 10
  1. #1
    Desh Banks is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Sending BufferedImage through socket

    Hello, thanks in advance to anyone reading and helping me out here.

    I have a simple client/server setup, in which the server reads in (using ObjectInputStream) a class I made that is a serialized container for a BufferedImage. The client needs to be able to send lots of images (screen captures) through the socket to the server as quickly as possible.

    Currently, when I try to send BufferedImage objects through quickly (with a sleep timer to every 100 ms or so), the client does not seem able to send the images quick enough, and eventually I get an OutOfMemoryException 0 out of heapspace.

    Is there an efficient way to send BufferedImage objects through the stream? I do NOT care about lost quality, so if I can reduce the quality thus reducing image size to increase transfer speed, that is OK.

    Thanks again.

  2. #2
    Desh Banks is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Update: I have been working on this problem, and have determined a few things...

    It seems the heapspace error is caused in/by my serialized class that stores the buffered image as array during transfer through the socket stream. This happens after about 111/109 images are sent from the client side (the client side then fails). Each image is roughly 177kb. I have tried using TYPE_RGB_GRAY to store the image, since I would think this takes less space, but it doesn't seem to have an effect on the filesize.

    Here is my serialized class used to send images from the client to the server:

    (Part of this example taken from a forum talking about serialized BI transfer)
    Java Code:
    package shared;
    
    
    import java.awt.image.BufferedImage;
    import java.io.Serializable;
    
    public class ScreenCapture implements Serializable
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private int[] _bytesOut = null;
    	private int _height, _width;
    	
    	public ScreenCapture( BufferedImage bi )
    	{
    		_height = bi.getHeight();
    		_width = bi.getWidth();
    		_bytesOut = new int[_width * _height];
    		
    		 bi.getRGB( 0, 0, _width, _height, _bytesOut, 0, _width );
    	}
    	
    	public BufferedImage getImage()
    	{
    		BufferedImage bi = new BufferedImage( _width, _height, BufferedImage.TYPE_BYTE_GRAY );
    		bi.setRGB( 0, 0, _width, _height, _bytesOut, 0, _width );
    		return bi;
    	}
    }
    The heapspace failure happens at two places that I can see:

    This allocation: BufferedImage bi = new BufferedImage( _width, _height, BufferedImage.TYPE_BYTE_GRAY );

    and this one: _bytesOut = new int[_width * _height];

    (it is not always consistent which alloc fails).

    To me this suggests that something isn't getting freed up by the garbage collector... Is this a common problem with serialized objects/objects sent through a stream?


    **It has occurred to me perhaps one reason this isn't getting any responses is because it looks to be a malicious goal - I can understand that.

    This is part of a program I started writing yesterday that takes screen captures of a game, sends them to someone else on the local (or external I suppose) network. It simulates having a screen of the other person's game right next to them, without needing to have a long VGA cable).

  3. #3
    mame is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Hi desh im having the same problem? Have u find a fix for it??
    Code snip where error occur
    While(true){
    ImageIcon imageIcon = (ImageIcon) objectInputStream.readObject();
    //……
    BufferedImage buffered = new BufferedImage(
    imageIcon.getIconWidth(),
    imageIcon.getIconHeight(),
    BufferedImage.TYPE_INT_RGB
    );
    //……
    }

    On each loop a new BufferedImage will be created and I don’t think the previous is freed from memory? So is there a manual freeing method like C++? Or just let it crash ever 6 frame…
    Btw I don’t think it got nothing to do with the network speed this is just a heap space error

  4. #4
    Desh Banks is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Indeed I found the cause of my error. Apparently, it turns out that the output stream (or something underneath it) maintains a reference to all objects it has seen - which means that the java garbage collector is unable to free the memory for them. To force the output stream to release its references, you can call:

    ObjectOutputStream.reset(); // force release of references (pointers) to stored objects it has sent

    Let me know if this helps. It fixed the problem for me.

    Good luck.

    Edit: In your case, you may be able to fix the problem in that loop by calling a similar method on the ObjectInputStream object after you are done with it.
    Last edited by Desh Banks; 01-26-2011 at 08:53 PM.

  5. #5
    mame is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    thanks, the buffer doesnt have a reset or dispose and i have tried the ObjectInpustStream.reset() which make it lose the connection and it isnt the reason.

  6. #6
    Desh Banks is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Maybe you would have to try a more round about way to lose the references. Try crating new input streams every so often, and maybe that will get the object to lose its references for the garbage collector.

  7. #7
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    sending bufferimage using the write function of objectoutputstream class i think is not possible but you can create another class which contains a bufferedimage.. then you can send the class you created through then socket..

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

    Default

    Quote Originally Posted by syst3m.tr0jan View Post
    sending bufferimage using the write function of objectoutputstream class i think is not possible but you can create another class which contains a bufferedimage.. then you can send the class you created through then socket..
    No, that's not true; a BufferedImage indeed doesn't implement the Serializable interface but wrapping it in another class (as a member variable) doesn't all of a sudden make it Serializable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    yeah.. i forgot to add-up about the what class to implement.. sorry.. let me rephrase..

    sending bufferedimage through socket can be done by creating a class that contains the bufferedimage and implements the serializable class..

    hehe.. i forgot to elaborate this thing because on my code i changed the bufferedimage to ImageIcon..

    sorry again. cheers..

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

    Default

    Quote Originally Posted by syst3m.tr0jan View Post
    yeah.. i forgot to add-up about the what class to implement.. sorry.. let me rephrase..

    sending bufferedimage through socket can be done by creating a class that contains the bufferedimage and implements the serializable class..
    That doesn't work either; for a class to be Serializable it has to implement the (empty) interface Serializable (or a superclass has to do it) and, most important, the members that have to be serialized have to implement the Serializable interface as well (or defined as transient). A BufferedImage can not be serialized because it doesn't and none of its superclasses do, implement the Serializable interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Sending object through socket
    By Alexandrinne in forum New To Java
    Replies: 0
    Last Post: 11-15-2010, 07:03 AM
  2. Problems with sending zip file using Socket
    By morita in forum Networking
    Replies: 0
    Last Post: 05-14-2010, 06:41 PM
  3. sending object through network/socket
    By skandalouz in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:34 AM
  4. Sending a file through socket
    By sureshkumarcs88 in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:32 AM
  5. problem in socket connection in sending images
    By vibhor in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 05:39 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
  •