android cpu过高的原因,关于android 进程CPU占用率高的原因分析
*記錄開發中的問題,方便以后自已回憶,2017-07-17 記錄
在開發一個語音的線程中,發現有如下事情,系統越來越慢,也越來越卡,打到
adb shell 輸 入 top 查看系統資源,發現 CPU占用率節節升高.我的去。*
最終發現是線程沒有關閉,而不停的創建新的線程 ,
private class TrackThred extends Thread {
private byte[] TTSResult;
private volatile boolean stop = false;
private List bytes = new ArrayList<>();
private boolean isTrack = true;
private void setTTS(byte[] ttsResult) {
bytes.add(ttsResult);
}
@Override
public void run() {
super.run();
audioTrack.play();
//audioTrack.getPlaybackHeadPosition();
int i = 0;
**while (isTrack&&!stop) {** //這個地方沒有加控制開關,在重新 new TrackThred 時發現多次創建的問題
if(i < bytes.size()-1) {
TTSResult = bytes.get(i);
audioTrack.write(TTSResult, 0, TTSResult.length);
i++;
}
}
}
public void onStop() {
isTrack = false;
audioTrack.stop();
}
}
130|root@MagicBox_M16S:/ # top | grep “tvtaobao”
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
7861 7 0% S 86 1646780K 108312K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 0% S 86 1646844K 108816K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 3% S 86 1646780K 109228K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 16% R 104 1703404K 152892K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 2 18% S 109 1730100K 183492K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 2% S 112 1722144K 172400K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 3% S 112 1722784K 169596K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47724K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 11% S 115 1727064K 155580K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47760K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 6% S 121 1718892K 166352K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 11% S 124 1768548K 190136K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 0% S 124 1768548K 189696K fg u0_a121 com.yunos.tvtaobao
6878 4 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 124 1768548K 189916K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 0% S 124 1768612K 190068K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 0% S 124 1768776K 190020K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 0% S 124 1768776K 190096K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 4% S 124 1768740K 190064K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 16% S 125 1769604K 193096K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 13% S 125 1769644K 193108K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 13% S 125 1769876K 193340K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769876K 193340K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769876K 193276K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 13% S 125 1769684K 193056K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 23% S 124 1768628K 193192K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 28% S 124 1768628K 196344K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 28% S 124 1768820K 197860K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 25% S 124 1768820K 197880K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 25% S 122 1766516K 197704K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 25% S 122 1766556K 197704K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 25% S 119 1763452K 198348K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 31% S 119 1763580K 198468K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 1 40% S 120 1765848K 207696K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 39% S 120 1764440K 202820K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 37% S 120 1764440K 199952K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 36% S 120 1764248K 199964K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 37% S 120 1764312K 200072K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 37% S 120 1764440K 200168K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 43% S 120 1764440K 200136K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 50% S 120 1767884K 205048K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 52% S 120 1764288K 202728K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 49% S 120 1764288K 202660K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 48% S 120 1764288K 202944K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 50% S 120 1764352K 202820K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 50% S 120 1764480K 202956K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 52% S 120 1764480K 202924K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 63% R 121 1769292K 205708K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 68% S 121 1765536K 202396K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 61% S 121 1765384K 202184K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765384K 202144K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765384K 202156K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 62% S 121 1765448K 202188K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 62% S 121 1765612K 202316K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 7 75% S 122 1770352K 207652K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 76% R 121 1765384K 204012K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 6 73% S 122 1766552K 204028K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 71% S 122 1766552K 203972K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 74% S 122 1766552K 201912K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 83% S 122 1766400K 201744K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 83% S 122 1766400K 201744K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767520K 201604K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 84% S 123 1767456K 202580K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767456K 202520K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 84% S 123 1767456K 203044K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 5 80% S 123 1767456K 203056K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 80% S 123 1767520K 203088K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 4 85% S 123 1767456K 202812K fg u0_a121 com.yunos.tvtaobao
6878 7 0% S 38 1548356K 47912K bg u0_a121 com.yunos.tvtaobao:channel
7861 1 83% S 124 1768552K 205384K fg u0_a121 com.yunos.tvtaobao
^C
package com.yunos.tvtaobao.activity.AudioDemo;
import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Handler;
import android.widget.Toast;
import com.alibaba.idst.nls.NlsClient;
import com.alibaba.idst.nls.NlsListener;
import com.alibaba.idst.nls.internal.protocol.NlsRequest;
import com.alibaba.idst.nls.internal.protocol.NlsRequestProto;
import com.yunos.juhuasuan.common.Log;
import com.yunos.tv.lib.LOG;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
/**
* Created by yugangjiang on 2017/7/7.
*/
public class Nlscontent {
private static final String TAG = “Nlscontent”;
private TrackThred thred;
private NlsClient mNlsClient;
private NlsRequest mNlsRequest;
private static String str_tts;
private Context context;
public Handler m_handler;
public List m_list = new ArrayList<>();
public static String current_key =”“;
public static int current_pos = 0 ;
public static int audioTrack_pos = 0 ;
public static int audioTrack_pos_count = 0;
//定義
public Boolean TTS_open = false;
public Boolean is_replay_true = false;
int iMinBufSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,
iMinBufSize, AudioTrack.MODE_STREAM) ; //使用audioTrack播放返回的pcm數據
private static Nlscontent nlscontent = null;
public static Nlscontent getInstance() {
if (nlscontent == null) {
synchronized (Nlscontent.class) {
if (nlscontent == null)
nlscontent = new Nlscontent();
}
}
return nlscontent;
}
public void getStrList(String inputString, int size,List list) {
if(inputString.length() < size)
{
list.add(inputString);
}else
{
list.add(inputString.substring(0,size));
getStrList(inputString.substring(size,inputString.length()),size,list);
}
//return getStrList(inputString, size);
}
public void init(Context context,Handler m_handler)
{
this.context = context;
this.m_handler = m_handler;
TTS_open = true;
mNlsRequest = initNlsRequest();
String appkey = "nls-service-streaming"; //請設置簡介頁面的Appkey
mNlsRequest.setApp_key(appkey); //appkey請從 簡介頁面的appkey列表中獲取
mNlsRequest.initTts(); //初始化tts請求
NlsClient.openLog(true);
NlsClient.configure(context.getApplicationContext()); //全局配置
mNlsClient = NlsClient.newInstance(context, mRecognizeListener, null ,mNlsRequest);
}
//重播
public Runnable replay_Run = new Runnable() {
@Override
public void run() {
if(is_replay_true) {
play_String(current_key);
}
}
};
private NlsRequest initNlsRequest() {
NlsRequestProto proto = new NlsRequestProto();
proto.setApp_user_id("user_id"); //設置用戶名
return new NlsRequest(proto);
}
public void play_String(String m_TTs)
{
TTS_open = true;
mNlsRequest.setTtsEncodeType("pcm"); //返回語音數據格式,支持pcm,wav.alaw
mNlsRequest.setTtsVolume(100); //音量大小默認50,閾值0-100
mNlsRequest.setTtsSpeechRate(-100);//語速,閾值-500~500
mNlsClient.PostTtsRequest(m_TTs); //用戶輸入文本
Log.d(TAG,"playTts ===" + m_TTs + " length: " + m_TTs.length());
str_tts = m_TTs;
current_key = m_TTs;
mNlsRequest.authorize("lbxirhq7cO1Gc2dG", "terZhCBs8Wv8Gswx1pHevS7zwEN9N2"); //請替換為用戶申請到的數加認證key和密鑰
}
public void playTts(String user_input){
if(user_input.length()<=0)
{
return;
}
current_pos = 0 ;
m_list.clear();
getStrList(user_input,200,m_list);
play_String(m_list.get(0));
}
public Runnable play_run = new Runnable() {
@Override
public void run() {
//如果第一次要播放
if(audioTrack!=null)
{
if(audioTrack.getPlayState()==AudioTrack.PLAYSTATE_STOPPED)
{
audioTrack.setPlaybackHeadPosition(0);
}
}
}
};
private void destroyThread(TrackThred thread) {
try {
if (null != thread && Thread.State.RUNNABLE == thread .getState()) {
try {
Thread.sleep(500);
thread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
BlockingQueue bq;
} catch (Exception e) {
e.printStackTrace();
} finally {
thread = null;
}
}
public int getAudioPosition()
{
if(audioTrack !=null)
{
return audioTrack.getPlaybackHeadPosition();
}
return -1;
}
public void stopTTS() {
Log.d(TAG,"stopTTS ===");
if (thred != null && thred.isAlive()) {
thred.onStop();
thred = null;
}
}
private NlsListener mRecognizeListener = new NlsListener() {
@Override
public void onTtsResult(int status, byte[] ttsResult){
Log.w(TAG, "NlsListener onTtsResult status : " + status);
switch (status) {
case NlsClient.ErrorCode.TTS_BEGIN :
if(thred!=null)
thred.stop = true;
thred = new TrackThred();
Log.e(TAG, "tts begin");
break;
case NlsClient.ErrorCode.TTS_TRANSFERRING :
Log.e(TAG,"tts transferring"+ttsResult.length);
thred.setTTS(ttsResult);
break;
case NlsClient.ErrorCode.TTS_OVER :
Log.e(TAG,"tts over");
// Nlscontent.getInstance().m_handler.post(m_Runnable);
thred.start();
is_replay_true = false; //走到這個地方了一定是播放成功了
// initNlsRequest();
break;
case NlsClient.ErrorCode.CONNECT_ERROR :
Log.e(TAG,”tts error”);
break;
case NlsClient.ErrorCode.SERVER_HANDLING_ERROR:
Log.e(TAG,”network—-500”);
break;
}
}
public void onRecognizingResult(int status, NlsListener.RecognizedResult result) {
Log.d(TAG," onRecognizingResult status== " + status + " result : "+ result);
if((status == 1 || status == 500 || status == 530 || status ==408) && (result == null))
{
//如果因為sockert的原因,沒有播成功,就重播一下...
is_replay_true = true;
m_handler.postDelayed(replay_Run,1000);
}
}
public void onServiceStatChanged(boolean isStreamAvailable, boolean isRpcAvailable) {
Log.d(TAG," onServiceStatChanged isStreamAvailable : " + isStreamAvailable + " isRpcAvailable : "+ isRpcAvailable);
}
};
public int getTTSState() {
return thred.getState().ordinal();
}
private class TrackThred extends Thread {
private byte[] TTSResult;
private volatile boolean stop = false;
private List bytes = new ArrayList<>();
private boolean isTrack = true;
private void setTTS(byte[] ttsResult) {
bytes.add(ttsResult);
}
@Override
public void run() {
super.run();
audioTrack.play();
//audioTrack.getPlaybackHeadPosition();
int i = 0;
while (isTrack&&!stop) {
if(i < bytes.size()-1) {
TTSResult = bytes.get(i);
audioTrack.write(TTSResult, 0, TTSResult.length);
i++;
}
}
}
public void onStop() {
isTrack = false;
audioTrack.stop();
}
}
}
總結
以上是生活随笔為你收集整理的android cpu过高的原因,关于android 进程CPU占用率高的原因分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MultiCD: 构建多重引导 CD 映
- 下一篇: 便宜5万