Drop_caches by app doesn't work

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

I've made this script but doesn't work:

package com.mkyong.android;  import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import java.io.IOException; import com.example.toast.R;  public class MainActivity extends Activity {   private Button button;  public void onCreate(Bundle savedInstanceState) {     final Runtime runtime = Runtime.getRuntime();     try {         runtime.exec("su");     }     catch (IOException e) {         e.printStackTrace();     }     super.onCreate(savedInstanceState);     setContentView(R.layout.tab1);       button = (Button) findViewById(R.id.button1);     button.setOnClickListener(new OnClickListener() {         @SuppressLint("SdCardPath")         @Override         public void onClick(View arg0) {             final Runtime runtime = Runtime.getRuntime();             try {                 runtime.exec("echo 3 > /proc/sys/vm/drop_caches");                 Toast.makeText(MainActivity.this, "Script lanciato con `successo, memoria svuotata.", Toast.LENGTH_LONG).show();`             }             catch (IOException e) {                 e.printStackTrace();             }         }     }); } } 

Doesn't free the RAM memory :( but via terminal emulator goes..If i try to change command and for example, make a dir with mkdir goes, goes even the writing of a file txt.. what's wrong?

回答1:

Your runtime.exec("su"); just started a shell process. And your next "runtime.exec("echo 3 > xxx")"; is not executed in the first shell.

My suggestion is to stick with java.lang.process, start a process which executes "su" and use the redirected stdin to write your command to it.



回答2:

You could try this.

try { Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "echo 3 > /proc/sys/vm/drop_caches" }); proc.waitFor(); } catch (Exception e) { Log.d("Exceptions", "Exception dropping caches: "+e); } 

OR

            Process p=null;             try {                 p = new ProcessBuilder()                 .command("PathToYourScript")                 .start();             } catch (IOException e) {                 e.printStackTrace();             } finally {                 if(p!=null) p.destroy();             } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!