Wintellect Blogs

Pooling Buffers for Better Memory Management

Sergio Loscialo

9 Jun , 2015  

Occasionally, you need a more robust solution to solve a problem.

In my last post, I wrote about the horrors of this small code snippet:

public byte[] Serialize(object o)
    using (var stream = new MemoryStream())
        MySerializer.Serialize(stream, o);
        return stream.ToArray();

One way to alleviate the memory pressure that can be caused by frequent creation and destruction of large objects is to tell the .Net garbage collector to compact the Large Object Heap (LOH) using:

GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;

However, this solution, while it may reduce the memory footprint of your application, does nothing to really solve the initial problem of allocating all that memory in the first place. As I mentioned in my last post, one way to accomplish that goal is to use a buffer pool.

The Buffer Manager

Instead of writing my own buffer manager, I am going to use the implementation Microsoft provides in the Windows Communication Framework (WCF) called the BufferManager. Now that WCF is open source you can go look at the implementation of BufferManager here.

The BufferManager essentially allocates a large chunk of contiguous memory and sets it aside for later use. You can create an instance of a BufferManager by calling the CreateBufferManager() static method as follows:

BufferManager.CreateBufferManager(maxPoolSize, maxBufferSize);

The first parameter, maxPoolSize represents how much memory you want the BufferManager to allocate in total.… Read more

, ,

Wintellect Blogs

Hey, Who Stole All My Memory?

Sergio Loscialo

16 May , 2015  

Sometimes a little piece of seemingly innocuous code can cause a significant amount of trouble:

public byte[] Serialize(object o)
    using (var stream = new MemoryStream())
        MySerializer.Serialize(stream, o);
        return stream.ToArray();

Doesn’t look like much, but I’m sure we have all written something like this and paid it no mind. In fact, most of the time, code like this isn’t a problem.

This little method becomes a problem when that object o parameter is larger than 85k (actually 84,988 bytes) in contiguous memory. At that point, this object is considered to be a “large object.” That designation is significant since when you’re done with an object like this, the .Net garbage collector stores it on the Large Object Heap (LOH) rather than in the gen 0 heap.

A situation called LOH fragmentation arises when applications are frequently creating and disposing many large objects. A classic example is a file upload service that receives and processes large volumes of data in each request. The resulting fragmentation can lead to an OutOfMemoryException being thrown, even if the server still has plenty of memory and there’s plenty of space left in the process address space.

So, getting back to our little innocuous piece of code: we already know that if o is a large object, it will consume space in the LOH.… Read more


Wintellect Blogs

Windows 8 GridView and Variable-Sized Items

Sergio Loscialo

8 Mar , 2013  

The GridView control is one of the most used layout controls within Windows 8 Store applications.  By using the Grid App project template with Visual Studio 2012, your first page will look similar to the following:

GridView Figure1

While this looks great, what if you’d like to create a items that vary in size?  Something like the following:

GridView Figure2

This type of layout is possible using the GridView with very little extra work.   There are multiple methods for accomplishing this task; some easier than others, some with more code than Xaml.

One way would be to subclass the GridView control and put in the necessary plumbing. See Jerry Nixon’s post.

Another way would be to follow Andrej Torzon’s advice using attached properties, reflection and a binding helper.

My solution, however, was to minimize the extra code by using the capabilities already present in the GridView.  To get started, the first thing to do is to tell the GridView to use a VariableSizedWrapGrid.  You do that by adding as follows: 

Step 1 is to create a new ItemsPanelTemplate for the GridView (I like to create separate resources, but this could be embedded in the GridView Xaml itself).

<ItemsPanelTemplate x:Key="VariableSizedItemTemplate">
    <VariableSizedWrapGrid Orientation="Vertical" />

Step 2 requires the default Style for the GridViewItem control, which you can find here, or extract using a tool like Expression Blend.   If there is a way to base a new style off of an implicit one, I haven’t found it.  Once you have the defined style, turn it into an explicit style by giving it a Key.   I am using the following snippet:

<Style x:Key="DefaultGridViewItemStyle"
Read more

Wintellect Blogs

Programmatically uninstalling Silverlight Out-Of-Browser Application

Sergio Loscialo

21 Feb , 2012  

Every once in a while there comes a need to provide a mechanism to uninstall your application programmatically.   For Silverlight Out-Of-Browser applications , there seemed to be no way to accomplish it.  Fortunately, there’s a work-around that trusted OOB applications running in Windows can use.

Silverlight OOB applications are launched via the “sllauncher.exe” command.  And if you open the properties on an OOB shortcut, you will see a target textbox with something like:

"C:\Program Files (x86)\Microsoft Silverlight\sllauncher.exe" 2205248641.localhost

Of course, “sllauncher.exe” has some command-line parameters and settings we can use to install and uninstall Silverlight OOB applications.  Since trusted Silverlight OOB applications can access COM Automation, we can run the “sllauncher.exe” application from within Silverlight.  The catch is that we’re not sure exactly where the program is located.

There may be exceptions, but as a general rule “sllauncher.exe” is located in 1 of 2 places:

on 64-bit machines : C:\Program Files (x86)\Microsoft Silverlight\sllauncher.exe

on 32-bit machines : C:\Program Files\Microsoft Silverlight\sllauncher.exe

However, there’s no way to know which OS type you’re running on.  So we need to check for the existence of these folders.  Since they’re out of the sandbox, we have to resort to COM Automation to determine if they exist.… Read more

Wintellect Blogs

Working with PivotViewer and the CxmlCollectionSource

Sergio Loscialo

17 Oct , 2011  

Microsoft’s PivotViewer control is an amazing tool for visualizing data and creating a unique UI for your application.  The one issue everyone seems to have with it, however, is its lack of styling capability.  In fact, working with PivotViewer in Expression Blend yields little in the way of styling or templating.  There are only a few color settings one can make, and some generic overall changes.   Replacing the filter panel or info panel is not currently supported.

What to do?

Since we cannot style the filter panel, we need to hide it and put our own in.  Hiding the panel is relatively simple and straight-forward.  Simply put, you need to subclass the PivotViewer and override the OnApplyTemplates() method, then walk the tree until you find the filter panel.  Fortunately, tools like Snoop or Mole can help you determine where the filter panel resides.  Below is a snippet of code showing where the filter panel resides in the Silverlight 5 version of the PivotViewer.

   1: public override void OnApplyTemplate()
   2: {
   3:     base.OnApplyTemplate();
   5:     Grid partContainer = (Grid)this.GetTemplateChild("PART_Container");
   6:     CollectionViewerView cvv = ((CollectionViewerView)(partContainer).Children[2]);
   7:     Grid container = cvv.Content as Grid;
   8:     Border viewerBorder = container.Children[1] as Border;
   9:     Grid viewerGrid = viewerBorder.Child as Grid;
  11:     // Filter Panel
  12:     viewerGrid.Children[2].Visibility = System.Windows.Visibility.Collapsed;
  13: }

NOTE: This is very brittle code and will most likely fail when Microsoft releases a new version of the PivotViewer.Read more

Wintellect Blogs

The Cascade Game: From Silverlight to HTML5 (part 2)

Sergio Loscialo

15 Sep , 2011  

Game Loops and Timer-Based Animations

Previous Entries in Series

My next short term goal was to get some of spheres to show and animate. And that was going to entail using HTML5’s new <canvas> tag. I saw two ways of going about animating the spheres. The first way would be to manually draw the spheres on a single canvas and animate them by clearing the canvas and redrawing at every game cycle. The second way would be to create a separate canvas for each sphere and use CSS3 to animate them. The latter method would allow the GPU to help accelerate the graphics processing and seems to be the best choice for animations of this type in HTML5 (see this blog entry from Jeff Prosise).

In the Silverlight version of this game, I created several classes that managed the spheres, levels, scores, and whatnot. I wanted to mimic that structure as closely as possible in Javascript to make the conversion less painful. Since Javascript doesn’t really have any type-safety without writing a lot of support code, I knew this task was going to be a bit frustrating until I became re-acclimated to Javascript again (I confess, it’s been some time).… Read more


Wintellect Blogs

The Cascade Game: From Silverlight to HTML5 (part 1)

Sergio Loscialo

8 Sep , 2011  

A few years ago I played a game on Facebook that had a simple premise, but became quite addictive for a while.  It consisted of progressing levels of spheres in which the player was tasked with exploding by initiating a chain-reaction.  You had one shot.  Placement, timing, and luck determined how well you scored.  At the time, I was learning Silverlight and thought it would be a good sample project to duplicate the fundamentals of this game in Silverlight, and thus Cascade was born.

Now, with the advent of Html5 and Javascript poised to reclaim the virtual landscape, I decided that it may be a beneficial learning experience to rewrite Cascade as an HTML5 application.   The next series of posts will outline my progression in converting the Silverlight version of Cascade into a workable Html5 game.

imageThe first thing I started with in the conversion was the shell for the game, a simple HTML5 page.  At this point, it’s still in the rough sketch phase (I’ll add more finalized artwork later).  Below is a picture of the game board with very little in the graphics department.   The HTML5 markup is pretty basic, but I wanted to put a gradient in the header.   With HTML5 and CSS3, gradients are very simple to implement without having to use a repeating graphic image.  So, while still possible to achieve this affect with either a large image or a small, repeated one, the new gradient effects in HTML5 /CSS3 are better for two reasons: they look good at all resolutions and are smaller in download size.… Read more


Wintellect Blogs

Where’s my DataContext?

Sergio Loscialo

26 Jul , 2011  

Every now and then a question comes up concerning a missing DataContext.  The questions usually center around a ViewModel being set to a View properly, but then developer can not bind some command or property to a FrameworkElement.  And typically the FrameworkElement in question is nested within an ItemsControl’s ItemTemplate or other similar structure.

First things first, once a DataContext is set, it is available for every child and all nested children until another DataContext is set.  That’s why the following Xaml snippet works, and we don’t need to set the DataContext on both TextBlocks.

   1: <Grid DataContext="{StaticResource MyViewModel}">
   2:     <StackPanel>
   3:         <TextBlock Text="{Binding SomeText}" />
   4:         <TextBlock Text="{Binding SomeMoreText}" />
   5:     </StackPanel>
   6: </Grid>

This is basic data-binding and is at the heart of WPF, Silverlight and Windows Phone 7 development.  So, no matter how many elements we nest within the Grid, they all will have access to the DataContext until an element is assigned its own DataContext.

The question of the lost DataContext usually arises when the Xaml is slightly more complex.  For example,

   1: <Grid DataContext="{StaticResource MyViewModel}">
   2:     <ItemsControl ItemsSource="{Binding MyItems}">
   3:         <ItemsControl.ItemTemplate>
   4:             <DataTemplate>
   5:                 <Grid>
   6:                     <StackPanel Orientation="Horizontal">
   7:                         <TextBlock Text="{Binding MyItemText}" />
   8:                         <Button Content="Delete" Command="{Binding DeleteItemCommand}" />
   9:                     </StackPanel>
  10:                 </Grid>
  11:             </DataTemplate>
  12:         </ItemsControl.ItemTemplate>
  13:     </ItemsControl>
  14: </Grid>

This can tend to trip up developers new to WPF and Silverlight.  Why? 

The first reason is that Grid on line 5 has had its DataContext changed transparently; the DataContext of the ItemsControl is still MyViewModel, but the internal grid has its DataContext set to an object in the bound collection (seen in line 2 as the ItemsSource).  In other words, the DataContext of the grid inside the ItemTemplate is an instance of an object in the collection MyItems.  For the above binding to work, the objects in MyItems would need have a property named MyItemText and a property called DeleteItemCommand.… Read more


Wintellect Blogs

Yet another way to attach ViewModels to Views

Sergio Loscialo

19 May , 2011  

Throughout the short history of the MVVM design pattern, several methods have originated for populating Views with their ViewModels.  One of the most common methods is to use one of the frameworks (PRISM, MVVM Light, Jounce, among others).  Typically, the ViewModel is registered with an IOC container and retrieved when needed.   One of the more outstanding issues is when to take the ViewModel and assign it to a View’s DataContext.  In many cases, the connection occurs in the constructor of the View with the View requesting the ViewModel from the IOC container.  In other cases, a locator is used as a static resource in the View’s Xaml, and using DataBinding a property of the locator is bound to the DataContext.  In the latter case, the locator requests the ViewModel from the IOC container.

In a recent project, I discovered (or rediscovered) another method for binding a ViewModel to a View.   Using Unity’s IOC Container, I registered both the View and the ViewModel together.  Granted this method will not work for every scenario, but in the cases where you create Views programmatically, I believe this method works perfectly.

Unity will allow you to register a type with a key which becomes important when registering different configurations of the same type.   But  more importantly, Unity supports configuration via InjectionProperties (and other methods as well).  InjectionProperties will enable Unity to resolve your Type and inject additional values into the generated class.  InjectionProperties work like this:

   1: int expectedInt = 8;
   2: container.RegisterType<MyClass>(
   3:     new InjectionProperty("IntProperty", expectedInt));

Basically, Unity will create an instance of MyClass and set the property “IntProperty” to 8, when the container resolves the type.… Read more

Wintellect Blogs

Silverlight Memory Leaks and AutomationPeers

Sergio Loscialo

13 Apr , 2011  

Using a memory profiling tool such as ANTS Memory Profiler from Red-Gate or MemProfiler from SciTech Software (I’m sure there others, but those were the only two I found that can profile a Silverlight Out-Of-Browser application) is a good idea.  By using such a tool, developers can discover a myriad of memory leaks and fix them before releasing their code.  These leaks usually stem from classes that attach event handlers to other classes.  In fact, in my opinion, I’ve seen more memory leaks in Silverlight (and WPF) stemming from poor event handler management than anything else.

However, every once in a while, you’ll see something that appears to be unexplainable.

After profiling an application for a client, I discovered the usual gambit of event-handlers and strong-references between objects.  In fact, an initial object retention graph (in this case, produced by the ANTS Memory Profiler) was almost indecipherable.


This is intentionally unreadable for two reasons: 1) to protect the innocent, and 2) because that graph is so big, there’s no way to actually show it in it’s entirety without massive scrolling (I could probably whip up a Deep Zoom for it, but that’s over kill for the purposes of this discussion).

The faint black box above the unreadable red “Start Here” box is the class I was researching, and the other boxes are classes in the hierarchy all the back to a GC root.  So, you can see that there were a lot.  After days of tracking down each strong reference and removing them, I ended up with a single chain.… Read more