
Originally Posted by
SB Dev
WP7.8 allows less memory for the PeriodicUpdateAgents than WP8 does. As for allowing the GC to collect no longer used objects. You have to ensure that everything you no longer need currently isn't referenced in any way by running code. e.g.:
Code:
var x = hugeObject;
doSomething(hugeObject);
x = null;//remove reference to hugeObject
GC.Collect();//would run by itself before an OOM occurs but you can give a hint to the Garbage Collector
doSomethingElse();
You have to make sure that there are NO references to the object that is no longer needed. If you don't set the reference to null it will stay alive until the method referencing it has returned (at which point the reference will be removed when the heap is cleaned up).
Thanks for the reply!
I just tried this but it doesn't seem to reduce the memory use. Did I do something wrong?
Here is my code in the background agent class.
Code:
protected override void OnInvoke(ScheduledTask task)
{
// dunno why but it starts with almost 5MB of memory...
var shellTile = ShellTile.ActiveTiles.FirstOrDefault();
// get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
if (shellTile == null)
return;
var data = new LiveTileData();
// GetData uses about 3MB of memory, it either gets the data from a web service or get it from Isolated storage
LiveTileDataManager.Current.GetData(data, () =>
{
CustomTile customTile;
UIElement frontTileTemplate, backTileTemplate, smallFrontTileTemplate, wideFrontTileTemplate, wideBackTileTemplate;
frontTileTemplate = new FrontTileTemplate
{
Property1 = data.Property1
};
backTileTemplate = new BackTileTemplate()
{
Property1 = data.Property1
};
smallFrontTileTemplate = new SmallFrontTileTemplate()
{
Property1 = data.Property1
};
wideFrontTileTemplate = new WideFrontTileTemplate()
{
Property1 = data.Property1
};
wideBackTileTemplate = new WideBackTileTemplate()
{
Property1 = data.Property1
};
// free up memory here???
data = null;
GC.Collect();
// pass in the small, normal & wide front tiles
customTile = CustomTile.GetTemplatedTile(frontTileTemplate, backTileTemplate, smallFrontTileTemplate, wideFrontTileTemplate, wideBackTileTemplate);
// this functions uses about 2MB of memory, it basically saves all tiles as JPEGs to isolated storage
var flipTileData = customTile.GetFlipTileData();
// free up memory here???
customTile = null;
frontTileTemplate = null;
backTileTemplate = null;
smallFrontTileTemplate = null;
wideFrontTileTemplate = null;
wideBackTileTemplate = null;
GC.Collect();
// invoke the new version of ShellTile.Update.
shellTileType.GetMethod("Update").Invoke(shellTile, new Object[] { flipTileData });
// tell the scheduled agent to complete
this.NotifyComplete();
});
}