Last week we did a video on background agents for the Inside Windows Phone show.. If you want to go deep into agents, you must watch the video. It is here.
To lure the super starPeter Torr into coming and doing yet another video on background agents, I had to prove to him the questions were deeper than what had already been covered in his knowledge chamber video and on his blog post series on agents. This is the email thread with the Qs and Answers that we covered in the video. The email thread on questions ended up being (imo) a good way to document the details, so sharing it below as a supplement to the video – again, go watch the video now.
Full disclaimer: these are implementation specific details that might change in the future; or might even change under specific circumstances in Mango. Use them (as a developer) to plan and design a great experience, but in the end it is still a phone, so code safely.
Q: How much memory does each agent get ?
Peak working set is 6MB for periodic and idle agents. Note that the runtime takes > 2MB of memory, so your agent really gets < 4MB.
When agents exceed the memory limit, even momentarily, they get terminated with or without an OOM exception (depending on whether OOM happened in managed or native memory).
For periodic and resource intensive agents, the scheduling service will auto-disable agents that crash or OOM twice in a row. Note that RI agents are also run in RR cycles.
Q: How much CPU does each agent get? what can people do to not exceed it..
t is a different story for audio and generic agents. For Audio: the OS ‘guarantees’ that the agent gets ~10% of CPU time every 5s. Depending on the foreground activity, the agent can use more – the average CPU is not limited in most cases).
Note that audio player and streaming agents share the same process and the resources (memory, CPU).
For periodic agents, expect to get 10-20% CPU in real time use due to parallel execution: we run up to 5 agents in parallel when the phone is locked.
This means an agent that uses L2S and networking and finishes in 15s may take more than 25s when run with other agents.
The CPU ‘guarantee’ here is 10% every 15s (which is rarely useful unless you are dealing with a foreground CPU-heavy app like IE). Again no CPU limit.
When there is low CPU activity in the foreground, agents can use as much CPU as they need.
One way to present this is: agents and foreground experiences share the same priority but run with different thread quantum allocation that ensures that, in most cases, the foreground app gets at least 70% of the CPU time.
Q:. How often do generic agents run?
Around every 28 minutes. See details below.
Q: What is the algorithm for generic agents? what is the battery threshold? Do they still run in battery saver mode?
Periodic agents don’t run when battery saver mode is On. They also don’t run when the phone battery is low (<20%). RI agents can’t run until the battery is at least 91%.
The periodic schedule is a little fuzzy to align with other scheduled network activities (for example email sync and push client keep-alive signals). It is scheduled to run every 28 minutes but the schedule can be invoked earlier in some cases (delta can be up to 10min). Also agents don’t run immediately after boot – they are delayed for ~30min.
Q: What is limit on # of agents in a phone? What error do devs get when that limit is reached?
No limit for Resource Intensive. Max of one audio agent at a time. For Periodic the limit is phone memory dependent. On devices with limited RAM (under 384MB), it is 9 agents, otherwise it is 15. You get an exception when the limit is hit (you have to check the error string unfortunately).
FAQ on things you can or can’t do from background agent:
Q:Can I pin a new tile?
Q: Can I update a pinned tile?
Yes, and you can delete pinned tiles
Q: Can I instantiate UIElements (even if not in a visual tree)? For example to create an image?
Yes, and you can probably also enable properties by adding controls to the ‘invisible’ RootVisual, e.g. with something like:((Panel)(Application.Current.RootVisual)).Children.Add(control);
You can use this for example to render user controls in the background (e.g. to create dynamic tiles). Watch out for the memory impact though always aim for <4.5MB peak.
Q: Can I create sockets, files, etc. open database, etc..
Yes, with one caveat: RI agents cannot go over cellular connection. This means RI agents will need a wifi connection to use sockets.
(The desktop connection is disabled when tethered for sockets in general, but that is more noticeable for RI agents).
Q: During debug, is it possible to run my agent more often than every 30 mins?
Yes, use the LaunhForTest APIs; Peter covered this in the video.
Happy Windows Phone coding. I will be back in full swing soon.