Results 1 to 10 of 10
- 11-20-2010, 09:23 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 11-21-2010, 04:44 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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)
The heapspace failure happens at two places that I can see: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; } }
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).
- 01-26-2011, 08:15 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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
- 01-26-2011, 08:50 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 01-27-2011, 06:12 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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.
- 01-27-2011, 07:54 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 01-30-2011, 09:45 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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..
- 01-30-2011, 12:22 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-30-2011, 01:02 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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..
- 01-30-2011, 05:05 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Sending object through socket
By Alexandrinne in forum New To JavaReplies: 0Last Post: 11-15-2010, 07:03 AM -
Problems with sending zip file using Socket
By morita in forum NetworkingReplies: 0Last Post: 05-14-2010, 06:41 PM -
sending object through network/socket
By skandalouz in forum NetworkingReplies: 1Last Post: 12-24-2009, 07:34 AM -
Sending a file through socket
By sureshkumarcs88 in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:32 AM -
problem in socket connection in sending images
By vibhor in forum NetworkingReplies: 2Last Post: 02-20-2009, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks