java - Android: Socket-Connection causes lags and a lot of GC action -
this thread socket-connection.
the server sends random int numbers socket (every 10 ms). problem is, after while huge lags of receiving data , logcat prints lot of "gc_concurrent freed" messages.
public class tcpsocketthread extends thread { context context; string ip; int port; public tcpsocketthread(context con, string ip, int port) { context = con; this.ip = ip; this.port = port; } @override public void run() { socket client; try { client = new socket(ip, port); bufferedreader in = new bufferedreader(new inputstreamreader( client.getinputstream())); string msg; while (true) { if (in.ready()) { msg = in.readline(); intent intent = new intent(bluetooth_data); intent.putextra(bluetooth_data_string, msg); context.sendbroadcast(intent); log.v("tcp-socket-thread", "msg: " + msg); } } } catch (unknownhostexception e) { log.v("client", "unknown host" + e); } catch (ioexception e) { log.v("client", "no i/o" + e); } } }
you should not repeatedly poll ready() test whether there data read. call readline(). block thread until either there data read or connection closed.
but don't think generating garbage. suspect that doing lines reading. understand using intents involves java object streams, , heavy-weight in terms of memory use in serialization , deserialization processes. , appears could doing deserialization in multiple places ... depending on listens broadcasted intents.
Comments
Post a Comment