2016-04-12 8 views
0

以下、第1回目と第2回目のアクティビティを表示しています。最初のものは、ベースラインメモリが何であるかを示すことです。私の質問は次のとおりです:何度も何度もメモリを割り当てている2番目のアクティビティが行われていますか?Androidのメモリが足りない

このように厳しい作業をしてはいけません。安定したペースで多くのメモリを割り当てているのはわかりません。それは、第2の画面のために、以下に示すレートで、それがなくなるまで割り振る。それから私は、GCが呼び出され、メモリを再割り当てすると思います。その後、メモリは何度も繰り返し実行されるまで割り当てられ続けます。

最初のアクティビティ画面:

First Screen

まず活動メモリ:

Memory for First Screen

第2の活性スクリーン: ませボタンがまだ押されていないと何もありませんビンgを記録した。 SignalStrengthListenerはLTEパラメータの変更をチェックし、1秒に1回UIを更新します。しかし、記憶は制御不能になっています。

Second Screen

セカンド活動メモリ:ここ

Memory for Second Screen

は、第二の活動のための私のコードとそのレイアウトです:

Second.java

public class Second extends Activity implements Runnable { 

    public static SignalStrengthListener signalStrengthListener; 
    public static TelephonyManager tm; 
    List<CellInfo> cellInfoList; 

    public static TextView lteRsrp; 
    public static TextView lteRsrq, lteCqi; 
    public static TextView cellPciTextView; 
    EditText offsetText; 

    Button startButton, offsetButton; 
    public static int cellPci = 0; 
    public static String ltestr; 
    public static String[] parts; 
    public static String mydate; 
    public static double offset = 0.0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 

     run(); 
     setupUI(); 
     setupButton(); 

    } 

    private void setupUI() { 
     lteRsrp = (TextView) findViewById(R.id.lteRsrp); 
     lteRsrq = (TextView) findViewById(R.id.lteRsrq); 
     lteCqi = (TextView) findViewById(R.id.lteCqi); 
     cellPciTextView = (TextView) findViewById(R.id.cellPciTextView); 
     offsetText = (EditText) findViewById(R.id.offsetText); 
     startButton = (Button) findViewById(R.id.startButton); 
     offsetButton = (Button) findViewById(R.id.offsetButton); 


     new Timer().scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 

         parts[9] = String.valueOf(Double.parseDouble(parts[9]) + offset); 

         if (Double.parseDouble(parts[9]) == 2147483647) { 
          parts[9] = "-141"; 
         } 
         if (Integer.parseInt(parts[10]) == 2147483647) { 
          parts[10] = "-21"; 
         } 
         if (Integer.parseInt(parts[12]) == 2147483647) { 
          parts[12] = "-20"; 
         } 

         lteRsrp.setText(String.valueOf(parts[9])); 
         lteRsrq.setText(String.valueOf(parts[10])); 
         lteCqi.setText(String.valueOf(parts[12])); 
         cellPciTextView.setText(String.valueOf(cellPci)); 
        } 
       }); 
      } 
     }, 0, 1000); 

    } 

    private void setupButton() { 
     startButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(getBaseContext(), Third.class); 
       startActivity(intent); 

      } 
     }); 

     offsetButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       offset = Double.parseDouble(offsetText.getText().toString()); 
       Log.d("TAG", "???????????????????????????????????????????????? offset value is = " + offset); 
       InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
       imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 
      } 
     }); 

    } 

    @Override 
    public void run() { 
     // Moves the current Thread into the background 
//  android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); 

     //start the signal strength listener 
     signalStrengthListener = new SignalStrengthListener(); 
     ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS); 

    } 

    public class SignalStrengthListener extends PhoneStateListener { 
     @Override 
     public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) { 

      ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS); 

      tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 

      ltestr = signalStrength.toString(); 
      parts = ltestr.split(" "); 

      try { 
       cellInfoList = tm.getAllCellInfo(); 
       for (CellInfo cellInfo : cellInfoList) { 

//      Log.d("TAG", "cellInfoList size = " + cellInfoList.size() + " +++++++++++++++++++++++++++++++"); 

        if (cellInfo instanceof CellInfoLte) { 
         // cast to CellInfoLte and call all the CellInfoLte methods you need 
         // Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown) 
         cellPci = ((CellInfoLte) cellInfo).getCellIdentity().getPci(); 
        } 
       } 
      } catch (Exception e) { 
//     Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e); 
      } 

      mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
      super.onSignalStrengthsChanged(signalStrength); 

     } 
    } 

} 

second_activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffdc1d"> 

    <TextView 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textSize="22sp" 
     android:textColor="#000000" 
     android:id="@+id/lteRsrp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginStart="29dp" 
     android:layout_marginTop="80dp" 
     android:textAlignment="textEnd" 
     android:background="#ffdc1d" 
     android:textStyle="bold" 
     android:layout_marginBottom="20dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="= LTE RSRP" 
     android:textSize="22sp" 
     android:textColor="#000000" 
     android:id="@+id/textView2" 
     android:background="#ffdc1d" 
     android:textStyle="bold" 
     android:layout_alignTop="@+id/lteRsrp" 
     android:layout_toEndOf="@+id/lteRsrp" 
     android:layout_marginLeft="10dp" /> 

    <TextView 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textColor="#a71b1b" 
     android:textSize="22sp" 
     android:id="@+id/lteRsrq" 
     android:layout_below="@+id/lteRsrp" 
     android:layout_alignStart="@+id/lteRsrp" 
     android:textAlignment="textEnd" 
     android:textStyle="bold" 
     android:background="#ffdc1d" 
     android:layout_marginBottom="20dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="= LTE RSRQ" 
     android:textSize="22sp" 
     android:textColor="#a71b1b" 
     android:id="@+id/textView3" 
     android:textStyle="bold" 
     android:background="#ffdc1d" 
     android:layout_below="@+id/lteRsrp" 
     android:layout_alignStart="@+id/textView2" /> 

    <TextView 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textSize="22sp" 
     android:textColor="#075f09" 
     android:id="@+id/cellPciTextView" 
     android:layout_below="@+id/lteRsrq" 
     android:layout_alignStart="@+id/lteRsrq" 
     android:textAlignment="textEnd" 
     android:background="#ffdc1d" 
     android:textStyle="bold" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="= LTE PCI" 
     android:textSize="22sp" 
     android:textColor="#075f09" 
     android:id="@+id/textView4" 
     android:background="#ffdc1d" 
     android:textStyle="bold" 
     android:layout_alignTop="@+id/cellPciTextView" 
     android:layout_alignStart="@+id/textView3" /> 

    <TextView 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textSize="22sp" 
     android:textColor="#000000" 
     android:id="@+id/lteCqi" 
     android:textAlignment="textEnd" 
     android:background="#ffdc1d" 
     android:textStyle="bold" 
     android:layout_below="@+id/cellPciTextView" 
     android:layout_alignStart="@+id/cellPciTextView" 
     android:layout_marginTop="20dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="= LTE CQI" 
     android:textSize="22sp" 
     android:textColor="#000000" 
     android:id="@+id/textView" 
     android:background="#ffdc1d" 
     android:textStyle="bold" 
     android:layout_alignTop="@+id/lteCqi" 
     android:layout_alignStart="@+id/textView4" /> 

    <Button 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="Start" 
     android:textColor="#ffdc1d" 
     android:textSize="22sp" 
     android:id="@+id/startButton" 
     android:layout_marginBottom="47dp" 
     android:background="#f91616" 
     android:textAlignment="center" 
     android:textStyle="bold" 
     android:padding="4dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Press the START button to begin recording" 
     android:id="@+id/textView8" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="#f91616" 
     android:textSize="22sp" 
     android:textStyle="italic" 
     android:textAlignment="center" 
     android:layout_marginTop="12dp" /> 

    <Button 
     android:layout_width="120dp" 
     android:layout_height="60dp" 
     android:text="Set offset" 
     android:id="@+id/offsetButton" 
     android:textColor="#ffdc1d" 
     android:background="#f91616" 
     android:textStyle="bold" 
     android:textSize="18sp" 
     android:layout_marginBottom="83dp" 
     android:layout_above="@+id/startButton" 
     android:layout_alignStart="@+id/textView" /> 

    <EditText 
     android:layout_width="120dp" 
     android:layout_height="40dp" 
     android:id="@+id/offsetText" 
     android:background="#ffffff" 
     android:layout_alignBottom="@+id/offsetButton" 
     android:layout_alignEnd="@+id/lteCqi" 
     android:text="0.0" 
     android:textAlignment="center" 
     android:textColor="#000000" 
     android:textSize="22sp" 
     android:textStyle="bold" 
     android:layout_marginBottom="4dp" /> 


</RelativeLayout> 
+1

割り当てトラッキングまたはヒープダンプを使用して、何が起こっているのかを判断します。 – CommonsWare

+0

私はそれをしましたが、トラッキングやダンプというのはそれほど意味がありません。私はSignalStrengthListenerへのネストされた呼び出しをたくさん見る。私はそれが犯人だと確信していますが、私は解決策を見つけられません。 – JParks

+1

さて、 'onSignalStrengthsChanged()'コールバックが呼び出されるたびに、一度だけ前から一度だけでなく一度だけ、 'listen()'を一度呼び出すようにしてください。 'getSystemService(Context.TELEPHONY_SERVICE)'を一回だけ呼び出す(各コールバックで一回ではなく、一回ではなく)、一回(各コールバックで一回ではなく)一回だけ 'getDateTimeInstance()'を呼び出すことも考えられます。 – CommonsWare

答えて

1

ええ、私はあなたがそこに無限ループのビットに自分自身を得たと思います。

The docs for listen()言う:登録時

、及び時に指定テレフォニー状態変化、テレフォニーマネージャは、リスナーオブジェクトに適切なコールバック・メソッドを呼び出し、現在の(更新)値を渡します。 onSignalStrengthsChanged()呼ばlisten()呼ばonSignalStrengthsChanged()呼ばlisten()呼ばonSignalStrengthsChanged()呼ばlisten()呼ばonSignalStrengthsChanged()呼ばlisten()呼ばonSignalStrengthsChanged()呼ば

ので

、何が起こったことは、あなたがlisten()と呼ばれていました、、、、、、、、、 、...

onSignalStrengthsChanged()メソッドの中からlisten()を削除すると、無限ループが削除されました。

+0

あなたは素晴らしいです!ありがとう!私は間違っていたので、onSignalStrengthsChangedの中で聴いていた()を取り出しました。それはまだ動作しますが、もっと重要なのは私の記憶です!私はもはや記憶がなくなりません!私は1ヶ月間それを見つめていましたが、私はDocsを読んでいましたが、それを理解していませんでした。コード上に目の第二のペアを持つことは非常に貴重です。 – JParks

関連する問題