Drop_caches from the application does not work

I made this script, but it does not 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();
            }
        }
    });
}
}

It does not free RAM memory :( but it goes through the terminal emulator. If I try to change the command and, for example, make dir with mkdir, even the txt file is written .. what's wrong?

0
source share
2 answers

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

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

0
source

You can 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();
            }
0
source

All Articles