RootShell icon indicating copy to clipboard operation
RootShell copied to clipboard

Memory leak at com.stericson.RootShell.execution.Shell.commands

Open LiuJiangshan opened this issue 7 years ago • 2 comments

LiuJiangshan avatar Jun 28 '18 11:06 LiuJiangshan

Hi,

Do you have any more information than what's in this report's subject? How did you observe a memory leak? Does your app run out of memory? If you are referring specifically to the commands list, then is it actually leaking memory of not returning an amount of memory, but with an upper bound?

Fusion avatar Jun 28 '18 17:06 Fusion

I created an example

MainActivity.java

package com.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.stericson.RootShell.RootShell;
import com.stericson.RootShell.exceptions.RootDeniedException;
import com.stericson.RootShell.execution.Shell;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MainActivity extends AppCompatActivity {

    static Shell shell;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (shell == null) {
            try {
                shell = RootShell.getShell(true);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (RootDeniedException e) {
                e.printStackTrace();
            }
        }
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        AppCompatButton button = new AppCompatButton(this);
        button.setText("test");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new TestFragment()).addToBackStack("").commit();
            }
        });
        linearLayout.addView(button);
        setContentView(linearLayout);
    }

    private Toast toast;

    public void toast(String msg) {
        if (toast != null) toast.cancel();
        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.show();
    }
}

TestFragment.java

package com.example;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stericson.RootShell.execution.Command;

import java.io.IOException;
import java.util.Random;

public class TestFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = new View(getContext());
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setBackgroundColor(Color.BLUE);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        try {
            MainActivity.shell.add(new Command(new Random().nextInt(), "ls /") {
                @Override
                public void commandOutput(int id, final String line) {
                    super.commandOutput(id, line);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((MainActivity) getActivity()).toast(line);
                        }
                    });
                }

                @Override
                public void commandCompleted(int id, int exitcode) {
                    super.commandCompleted(id, exitcode);
                    getActivity().getSupportFragmentManager().popBackStack();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

2018-06-29 9 31 48

LiuJiangshan avatar Jun 29 '18 01:06 LiuJiangshan