Memory Usage Problem 1.2-alpha2

Mar 24, 2014 at 10:40 AM
Edited Mar 24, 2014 at 11:16 AM
Hi henric,
I'm use phonesm-1.2-alpha2 dll library for my application. But i have some problem. Player is started about 47mb memory usage and my emulator have 5.08gb used storage, i am watcinh tv after 8minute memory usage is 87mb (if going to 180mb program crashed for high memory usage), and stroge is 5.11gb used stroge, and when i'm exit player and my app stroge is not cleaned its already 5.11gb please help me.

Photo first time run:


After 8minute:


its saving other directory:
Mar 24, 2014 at 11:17 AM
same problem in curret beta phonesm-1.2.2 version :(
Mar 24, 2014 at 1:23 PM
This might be related to HttpClient (likely caused by the platform's HttpWebRequest implementation).

Out of memory: Windows Phone 8: Portable HttpClient causes OutOfMemoryException downloading large file

Storage consumption: Portable HttpClient downloads create unremovable temporary storage

If we are lucky, perhaps MS will have a new portable HttpClient that fixes some of these problem for the BUILD conference next week..?

I can't fix the OS, but I am working on some changes right now that will make it easier to change how stuff is read from the network. Instead of using HttpClient everywhere, phonesm will use a custom IWebReader interface that should be flexible enough to read from sockets or isolated storage or what-not. I plan on providing an HttpClient-based implementation and, hopefully soon thereafter, another implementation based on the C++ REST SDK. I might also do an implementation based on HttpClient for playlists and sockets for media segments (primarily for WP7). Anyhow, I expect to push the IWebReader change to CodePlex in a day or so. (I might change the name first.. IMediaReader?)
Apr 4, 2014 at 9:07 PM
Thanks for answer hendric, i am waiting for update Microsoft.Net.Http
Apr 4, 2014 at 10:55 PM
There was an update to HttpClient in the last few days. I haven't had a chance to check if it has fixed any of this, but I suspect that it has not (I did update the NuGet packages and pushed the change to CodePlex, but I've only done a quick smoke test).

From poking around, it appears that the cache brain damage is a fundamental problem with the device's HTTP stack. WP8.1 and Windows 8.1 add XHR_PROP_NO_CACHE to IXMLHTTPRequest2::SetProperty that lets one turn if off.

As for the out-of-memory problem, I've built a Windows Runtime Component that wraps the C++ REST SDK. I can play streams with it, but the code in http_win8.cpp has the same brain damage as HttpClient. It keeps allocating buffer blocks without bound as data arrives (fixable in the available C++) and it uses the same broken cache IXMLHTTPRequest2 implementation (again, apparently not fixed until WP8.1). There does not seem to be a simple way to fix the out-of-memory problem without either duplicating a good chunk of the C++ REST SDK code or by forking the project.

I haven't quite decided how to proceed. A C# or C++ HTTP implementation running directly on top of a network socket would be necessary to really fix things. The one positive right now, is that phonesm's web code is pluggable and can be customized without ripping into code spread all over the project.

And this is nearly two decades after the memo...
Apr 22, 2014 at 12:58 PM
I ported the HttpWebRequest code from Mono to WP8/StreamSocket and I can now do a large GET without blowing out all of memory. The "Download()" in (the same as mentioned in my post) turned into:
        async Task Download()
            var buffer = new byte[4096];

            var webRequest = SM.Mono.Net.WebRequest.Create("");

            using (var response = await webRequest.GetResponseAsync(_cancellationTokenSource.Token).ConfigureAwait(false))
                using (var responseStream = response.GetResponseStream())
                    for (; ; )
                        var length = await responseStream.ReadAsync(buffer, 0, buffer.Length, _cancellationTokenSource.Token).ConfigureAwait(false);

                        if (length < 1)

                        Interlocked.Add(ref _totalBytesRead, length);

                        await DoSomethingWithData(buffer, 0, length).ConfigureAwait(false);
The nasty mix of EAP, APM, and TPL was setting my teeth on edge and I don't need to be backwards compatible with anything, so I swapped as much as I could over to TPL (which made it quite a bit more work than I had anticipated). I'll put it on GitHub after cleaning things up a bit; it should be pretty simple to use it with phonesm. Given the scope of the changes, I suspect I may have added a bug or two.
Apr 23, 2014 at 12:57 AM
The ported HttpWebRequest code is here:
Apr 24, 2014 at 7:02 PM
thanks for solve this problem. I'm follow changes