我花了几天时间,阅读了很多有关此案的帖子,但尚未解决麻烦的问题。内存无限期增加几KB。在不进行任何通信的情况下连接到服务器一小时后,内存占用已从9.2 MB增加到10.6 MB,增加的是Java部分。这是服务:
package com.androidsrc.client;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketService extends Service {
public static final String SERVERIP = "192.168.0.11"; //your computer IP address should be written here
public static final int SERVERPORT = 1500;
PrintWriter outx = null;
int SDK_INT = android.os.Build.VERSION.SDK_INT;
public SocketService() {
}
public class LocalBinder extends Binder {
public SocketService getService() {
System.out.println("I am in Localbinder ");
return SocketService.this;
}
}
private final IBinder myBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return myBinder;
}
@Override
public void onCreate() {
super.onCreate();
if (SDK_INT > 8)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
System.out.println("I am in on create");
}
public void IsBoundable(){
Toast.makeText(this,"I bind like butter", Toast.LENGTH_LONG).show();
}
public void sendMessage(String message){
if (outx != null && !outx.checkError()) {// && !out.checkError()
System.out.println("in sendMessage "+message);
outx.print(message+System.getProperty("line.separator"));
outx.flush();
}
}
@Override
public int onStartCommand(Intent intent,int flags, int startId){
super.onStartCommand(intent, flags, startId);
System.out.println("I am in on start");
// Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
Runnable connect = new connectSocket();
new Thread(connect).start();
return START_STICKY;
}
class connectSocket implements Runnable {
private BufferedReader in = null;
private String content = null;
private Socket socket;
//InetAddress serverAddr;
private connectSocket () {
try {
//here you must put your computer's IP address.
//serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
//socket = new Socket(serverAddr, SERVERPORT, 2000);
socket = new Socket();
socket.connect(new InetSocketAddress(SERVERIP, SERVERPORT),21000);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outx = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while((content = in.readLine()) != null) {
Log.i("run----", "---------" + content);
//System.gc(); I tryed but no fortune
//break;//if this is uncommented second messagge not received
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
//socket.close();//if only declared global in class
} catch (Exception e) {
e.printStackTrace();
}
//socket = null;//if only declared global in class
}
}
这是增加内存的代码部分:
public void run() {
try {
while((content = in.readLine()) != null) {
Log.i("run----", "---------" + content);
//System.gc(); I tryed but no fortune
//break;//if this is uncommented second messagge not received
}
无限期拥有活动服务并始终收听以接收消息是最好的方法吗?我已经尝试过基于AsyncTask的类,在该类中,我已经解决了所有内存问题,可以实时侦听,但是在尝试使用该服务的时间内,不建议将AsyncTask用于很长的操作,但是我无法摆脱所示的问题。 预先感谢。
编辑: 我试图像这样使用Thread而不是Runnable:
class connectSocket extends Thread {
和
Thread connect = null;
connect = new connectSocket();
connect.start();
在onStartCommand中 我注意到,经过3分钟的不活动发送/接收内存后,尽管在Java和本机部分中这3分钟内持续增长,但下载后却下载了3分钟。 这是最好的方法吗?