Android StrictMode InstanceCountViolation -


i running app strictmode activated in development documented here strictmode lower platform versions , noticed error message not know think nor can find reference.

i android.os.strictmode$instancecountviolation values instances , limit e.g.

instances=3; limit=2

now wondering:

  • a) how limit calculated
  • b) how can such violation happen , evasive actions.

any ideas?

it's in code

the key strictmode.sexpectedactivityinstancecount , incrementexpectedactivitycount , decrementexpectedactivitycount:

so limit less each time activity destroyed, if instance leaked real instance count bigger limit, detect if it's leaked gc magic (in decrementexpectedactivitycount):

    system.gc();     system.runfinalization(); // added in https://github.com/android/platform_frameworks_base/commit/6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff     system.gc(); 

if after gc didn't remove activity app's memory considered leak.

conclusion

based on above way prevent make sure there no references offending activity after ondestroy. problem there may weakreferences still accessible through native objects, seem have different lifecycle. here's how came conclusion:

  • after backing out myactivity , seeing log message
  • make heap dump (.hprof)
  • open in eclipse memory analyzer
  • run oql: select * instanceof full.package.name.of.myactivity
  • select ctrl+click or shift+click
  • right click , merge shortest path gc roots > references

workaround

if increase count initially we'll have more legroom before reports leak specific classes:

// application.oncreate or nearby set strictmode detectactivityleaks method incrementexpectedactivitycount = strictmode.class.getmethod("incrementexpectedactivitycount", class.class) incrementexpectedactivitycount.invoke(null, myactivity.class); incrementexpectedactivitycount.invoke(null, myactivity2.class); 

further reading


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -