Android CPU burnout and memory leak

Supporting 4.1 and up
gary_bnry
Fresh meat
Posts: 4
Joined: Wed Jul 17, 2013 5:54 am
Contact:

Android CPU burnout and memory leak

Postby gary_bnry » Wed Jul 17, 2013 6:54 am

I have an MK808 with Android 4.2.2 (Custom ROM). (1.2Ghz Rockchip RK3066 Dual-Core CPU and 1GB DDR3 RAM)

I have two Phidgets 0/0/4 and 0/0/8 attached and open. I am switching outputs on and off with no problem. The problem is CPU and memory usage.

Using the DDMS tools in Android Developer Kit (ADT) I can see heavy CPU usage. The memory climbs 10MB/s (GC reduces most of it) while the CPU runs at a constant 18% until the app becomes unresponsive.

Looking at the threads (attached PNG) I notice that there are theads constantly being stoped and started. The bottom two correspond to the two Phidgets attahed to Android device. Within a few seconds the names of the bottom two threads change from Thread-53 to Thread-392.

(Please give this your urgent attention. I foresee a strong increase in the use of Phidgets with Android. This with the increase of Android media players and other 'less mobile' Android device.)
Attachments
threads.PNG
Phidget App threads
threads.PNG (24.98 KiB) Viewed 8263 times

User avatar
Patrick
Lead Developer
Posts: 3099
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Android CPU burnout and memory leak

Postby Patrick » Wed Jul 17, 2013 8:27 am

Are you opening/closing your phidgets a lot? Threads are only created/destroyed on open+attach and detach/close.

-Patrick

gary_bnry
Fresh meat
Posts: 4
Joined: Wed Jul 17, 2013 5:54 am
Contact:

Re: Android CPU burnout and memory leak

Postby gary_bnry » Fri Jul 19, 2013 4:57 am

Maybe I do not understand the "open+attach and detach/close" part well enough.

As seen in the code below I open each InterfaceKit instance with .open(serial). Attach happens automatically, but notifies the attach listener via AttachListener.attached(final AttachEvent ae). The Phidget is detached/closed by calling .close() Is this correct?

Code: Select all

public class PhidgetInterfaceService extends Service {
   private PhidgetDevices devices;

   @Override
   public void onCreate() {
      Manager.Initialize(this);
      devices = new PhidgetDevices();
      devices.setOnStateChange(...);
      devices.load();
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      return Service.START_STICKY;
   }

   @Override
   public void onDestroy() {
      devices.close();
      Manager.Uninitialize();
   }
}


Code: Select all

public class PhidgetDevices {
   private Map<String,PhidgetDevice> iks;

   public void load(){
      List<PhidgetDevice> devices = getDevicesFromTheDatabase();
      for (PhidgetDevice device : devices) {
         if (!iks.containsKey(device.getSerial())){
         iks.put(device.getSerial(), device);
         device.setOnStateChange(getStateChangeListener());
         device.open();
         }
      }
   }

   public void close(){
      for (Entry<String,PhidgetDevice> kv : iks.entrySet()){
         kv.getValue().close();
      }
   }

   private List<PhidgetDevice> getDevicesFromTheDatabase(){
      //initializes a List of PhidgetDevice and sets there property: serial
   }
}


Code: Select all

public class PhidgetDevice {
   public String serial;
   private InterfaceKitPhidget ik = null;

   public PhidgetDevice(){
      
   }

   public void open(){
      if (ik != null) close(); //should never be the case
      
      try {
         ik = new InterfaceKitPhidget();
      } catch (PhidgetException e) {
         e.printStackTrace();
      }

      ik.addAttachListener(new AttachListener() {
         public void attached(final AttachEvent ae) {
            //set local variables
            //notify observers
         }
      });
      ik.addDetachListener(new DetachListener() {
         public void detached(final DetachEvent ae) {
            //notify observers
         }
      });
      ik.addSensorChangeListener(new SensorChangeListener() {
         public void sensorChanged(SensorChangeEvent se) {
            //notify observers
         }
      });
      ik.addInputChangeListener(new InputChangeListener() {
         public void inputChanged(InputChangeEvent ie) {
            //notify observers
         }
      });
      ik.addOutputChangeListener(new OutputChangeListener() {
         public void outputChanged(OutputChangeEvent oe) {
            //notify observers
         }
      });
      try {
         ik.open(Integer.parseInt(serial));
         //ik.waitForAttachment();
      } catch (PhidgetException e) {
         e.printStackTrace();
      }
   }
   
   public void close(){
      try {
         Log.d("PhidgetInterfaceService", "opening device " + serial);
         if (ik.isAttached()) ik.close();
      } catch (PhidgetException e) {
         e.printStackTrace();
      }
   }
}


LogCat shows "opening device ####" once while the threads are constantly being recreated.

I hope this helps. The code above is cleaned up. If you like I could send you the whole app zipped up?

User avatar
Patrick
Lead Developer
Posts: 3099
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Android CPU burnout and memory leak

Postby Patrick » Fri Jul 19, 2013 8:17 am

What does the log actually look like? There should not be any extra threads being created other then at attach/detach.

-Patrick

gary_bnry
Fresh meat
Posts: 4
Joined: Wed Jul 17, 2013 5:54 am
Contact:

Re: Android CPU burnout and memory leak

Postby gary_bnry » Mon Jul 22, 2013 5:16 am

Hi Patrick

Thank you for your assistance. Attached is the LogCat output.

I uninsalled the app from the MK808 and cleared the log.

In the time of the log I intalled and run the app, added (opened/attached) both Phidgets. After a few seconds I uninstalled the app again.
Attachments
LogCat.txt
(212.4 KiB) Downloaded 246 times

User avatar
Patrick
Lead Developer
Posts: 3099
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Android CPU burnout and memory leak

Postby Patrick » Tue Jul 23, 2013 2:23 pm

Hi,

This issue appears to only affect apps being debugged on Android - new threads were not really being created, just the same threads being attached/detached to Java from native code. During debug, the thread references were not being cleaned up - but during normal operation they will be.

However - I have fixed the issue. You should notice a performance improvement, as I just leave the native threads attached until they exit. This does mean that you need to properly close phidget objects in your Java code before exiting, or the JVM may hang.

Latest release is out now.

-Patrick

gary_bnry
Fresh meat
Posts: 4
Joined: Wed Jul 17, 2013 5:54 am
Contact:

Re: Android CPU burnout and memory leak

Postby gary_bnry » Wed Jul 24, 2013 3:30 am

Thank you for the support, Patrick!


Return to “Android”

Who is online

Users browsing this forum: No registered users and 10 guests