简体   繁体   English

Android-App消耗大量能量。 插座可能是原因

[英]Android-App consume to much energy. Sockets maybe the reason

In my app I use a datagramSocket, one serversocket and several sockets. 在我的应用程序中,我使用一个datagramSocket,一个serverocket和几个套接字。 When my app runs on my android-device, the phone get hot on one area and it needs lots of energy. 当我的应用在我的android设备上运行时,手机在一个区域变热,并且需要大量能量。 I think the reason for the problem are my Sockets. 我认为问题的原因是我的套接字。 But Im not sure which opperation are so energy-consuming: 但是我不确定哪种操作会耗费大量能源:

Does it cost much energy to create new Sockets: 创建新的套接字是否需要花费大量精力:

Socket so = new Socket();
ss = new ServerSocket( port );
mSocket = new MulticastSocket( port );

Is it importand to close the serversocket and other sockets if I don't need it anymore. 如果我不再需要它,可以导入并关闭serversocket和其他套接字吗?

Does connecting cost energy? 连接会消耗能量吗?

so.connect( new InetSocketAddress(ip, port ), 1000);

And after connecting does the connection need much energy?(Is it better to disconnect when i don't send datas for a long time) Does a high amount of Sockets need much energy? 在连接之后,连接需要很多能量吗?(长时间不发送数据时断开连接会更好吗?)大量的Socket是否需要很多能量?

I also have several threads: 我也有几个线程:

Beside my main-thread and three binderthreads, I have three more: 除了主线程和三个绑定线程之外,我还有三个:

1st Thread: listen for incoming message on my datagram-socket: 第一个线程:在我的数据报套接字上监听传入的消息:

DatagramPacket packet = new DatagramPacket(buffer, buffer. length);
mSocket .receive(packet);

2nd Thread: listen for incoming messages on several sockets: 第二个线程:在多个套接字上侦听传入的消息:

try {
      if (objectInputStreamList .size() != 0){
            if (counter >= objectInputStreamList .size()){
                 counter = 0;
             }
             ObjectInputStream ois = objectInputStreamList .get(counter);
             Object o = ois.readObject();
             if (o instanceof Message){
                 receive((Message) o);
             System.out .println("Message received in Server");
                 }
             }
     } catch (SocketTimeoutException soTOE){
        counter++;
     }

3rd Thread: send Datas via an http-request via internet to a server. 第三线程:通过互联网通过http请求将数据发送到服务器。

Does one of the threads need much energy? 一根线需要很多能量吗?

The app needs also much energy even if wifi is turned off or wifi is turned on but not connected to wlan. 即使关闭wifi或打开wifi但未连接到wlan,该应用程序也需要大量能量。 And so there are no connections. 因此没有连接。

Which of this opperations are energyconsuming? 哪些操作消耗能量? And what can I do to reduce the energy? 我该怎么做以减少能量?

Thank you for your answers. 谢谢您的回答。

Given that it runs hot even when it's not supposed to do anything, I suspect one of your threads are running freely instead of properly blocking. 鉴于即使不执行任何操作它都可以运行,我怀疑您的线程之一正在自由运行,而不是适当地阻塞。 Can't tell you more without the complete source code, but some good old printf debugging might come in handy. 没有完整的源代码无法告诉您更多信息,但是一些不错的旧printf调试可能会派上用场。 Simply put different logging statements in each of your while loops and run the program. 只需在每个while循环中放入不同的日志记录语句,然后运行程序即可。 If a statement fills your log at an alarming rate, then there you have it. 如果一条语句以惊人的速度填满了您的日志,那么您就已经拥有了。

Yes you should always close a socket that you no longer need. 是的,您应该始终关闭不再需要的套接字。 For example, if you don't close your server sockets then their ports will remain occupied. 例如,如果不关闭服务器套接字,则它们的端口将保持占用状态。 Eventually the system will run out of ports. 最终,系统将用尽端口。 (I could come up with many other reasons, including resource usage and elegance. Tidying things up might even make the problem obvious.) (我可能会提出许多其他原因,包括资源使用和优雅。整理内容甚至可能使问题变得显而易见。)

FYI having many sockets open does not cost energy in and of itself. 供许多人打开的FYI本身并不消耗能源。 Network activity costs a little energy (pay attention to idle timers !), but it shouldn't make one part of your device really hot. 网络活动会消耗一点能量(请注意空闲计时器 !),但它不应使设备的一部分真正发烫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM