1. Are you ready for the Galaxy S20? Here is everything we know so far!

how to limit the CPU usage of an installed app.

Discussion in 'Android Help' started by Android Question, Sep 18, 2016.

  1. Android Question

    Thread Starter

    Device: galaxy s7 edge (no root)
    Apps: GNURoot – Debian (fake root) , Termux (terminal emulator)

    I am trying to run some cpu intensive linux software on my phone.
    I use the app “GNURoot – Debian” to get a linux terminal. Then I install and run my software.

    For a few minutes everything works as expected, but the software soon begins to use up to 300% cpu and the phone will freeze and require reboot.

    On my desktops I just use the command “cpulimit -P java -l 20” and this prevents the programs from crashing the desktop.

    On the GNURoot - Debian Terminal the command
    cpulimit -P java -l 20” will return “Proccess ID xxx detected” but the cpu usage is not actually limited. proccess cpu usage will still shoot up to 300% and crash the phone.

    “nice” or “renice” also seem to have no effect.

    I have tried to use the “Termux” terminal emulator to use the “cpulimit” command on the entire “GNURoot” app, but it returns “operation not permitted.

    I am no linux guru and I am guessing the problem might be that the phone is not rooted, but in my case rooting the devices is not an option.

    Can anyone please recommend how I can limit the cpu usage for this app or proccess? I’ve been stuck on this for over a week and am getting nowhere with the little time I have.

    Thanks for reading. Your help would be much appreciated.

    Kalegley likes this.

    1. Download the Forums for Android™ app!


  2. scary alien

    scary alien not really so scary
    VIP Member

    Yeah, I'm not even sure if you could do this if you were rooted, so the fact that you can't change the priority or impact how much CPU resources are consumed when not rooted it not surprising to me...
    Kalegley and svim like this.
  3. raydroid13

    raydroid13 Lurker

    Thank you "scary alien" I just tried to use "Kill -SIGSTOP ......" and it returned "operation not permitted". so I'm guessing you are right and cpulimit will not work (at least on a non rooted device)

    Any other possible approaches on how to prevent the java program from crashing the android system? something like a sandbox environment maybe?
    Kalegley likes this.
  4. raydroid13

    raydroid13 Lurker

    Oops. I thought my first reply didnt go through. :)
  5. scary alien

    scary alien not really so scary
    VIP Member

    Hmm, not sure, @raydroid13...are you seeing an actual crash (i.e., is a stack trace available)?

    Other thoughts--in no particular order to possibly assist with debugging:

    - watch the Android system logcat while your java program is running:

    - from a separate, Android terminal/shell session (Terminal Emulator for Android) or an adb shell from your PC:

    adb logcat
    - or -​
    logcat (from the shell)

    Redirect that output to a file for later review, of course. Also, there are some nice options for the logcat util to ask for a one-time output or a tail or a full dump of system messages, etc. (use logcat -? to see those)

    - if you were rooted, I would suggest viewing the kernel log (cat /proc/kmsg)...

    - if this is your Java program, you could instrument it with key displays at critical function points...

    Otherwise, not sure if there is a way to tell Java to output/display current state/status...run-time options?

    Maybe the strace utility? (not sure if that requires root or not)

    That's all I have for now...wish I had something concrete for you...
    Kalegley and svim like this.
  6. raydroid13

    raydroid13 Lurker

    Thank you much anyway. at least I can look for a different approach now. I will try to limit resource usage from within the software and see how effective that is.

    If anyone could confirm weather or not the cpulimit command works on a rooted device I would like to hear it.
    Kalegley likes this.
  7. scary alien

    scary alien not really so scary
    VIP Member

    No problem :).

    I'll charge-up my test, rooted Nexus 5 and see if I have the cpulimit command and how it behaves with and without root.
    Kalegley likes this.
  8. raydroid13

    raydroid13 Lurker

    Thank you I look forward to your answer. I suppose next I will have to try to build in some sort of resource limit in the code. Luckily I am using open source software.
    Kalegley and scary alien like this.
  9. scary alien

    scary alien not really so scary
    VIP Member

    Hmm, I don't see that either stock (Nexus 5, Android 6.0.1) or busybox has a cpulimit utility :(.

    I'm guessing it comes with your Android Linux distro...
    Guggy and Kalegley like this.
  10. raydroid13

    raydroid13 Lurker

    Yes Indeed it is part of the "GNURoot - Debian" App
    scary alien likes this.

Share This Page