Calvin Hsia's WebLog

thoughts from a professional developer

Browse by Tags

Tagged Content List
  • Blog Post: How to monitor and respond to memory use

    A colleague asked me how to run code in response to low memory condition. Apparently, data is buffered and can be flushed to disk or a server when memory gets low. So I showed him the code below. Start Visual Studio File->New->Project->C# WPF application Paste in the code below in the MainWindow...
  • Blog Post: See how often your code runs and how much time it takes

    Last post I showed how to create your own CLR profiler. This time, we’ll add a little code to run whenever a managed method is entered and exited. We’ll do this by signing up for the  Enter and Leave function hooks by calling ICorProfilerInfo2->SetEnterLeaveFunctionHooks2 . This will allow us...
  • Blog Post: Does the CLR release memory when no longer needed?

    A colleague asked the other day if the CLR releases memory when it’s no longer needed. Suppose you allocate lots of memory, then release it. The CLR will grow the managed heap (the green below), but it will also shrink it if it can. All memory in a process must come from a pool of virtual addresses,...
  • Blog Post: Turn your tests into stress tests easily

    It’s great to be able to write tests and execute them while developing a project. While I’m developing, I can hit a button and run the dozens of tests to see if I’ve broken anything. As code gets written lots of things get refactored, moved around, etc. Hitting a button to see if I broke anything is...
  • Blog Post: The number of Garbage Collections indicate how much memory is used

    One of the performance improvements we made in .Net was with System.Text.StringBuilder . StringBuilder is used in lots of code to build strings: it has various methods to modify strings quickly. Once a string is built, the ToString method is called to create the string. We observed via MemSpect that...
  • Blog Post: Performance of Memory vs Disk

    There is a comment on this Visual Studio Blog post (about how we made Visual Studio faster): “Focus on speed, not memory usage. Memory is very cheap, but CPU performance is muuuuuuuuch moooooooore expensive.” Yes, memory keeps getting cheaper, but actually, reducing memory use is critical to increasing...
  • Blog Post: Examine the layout of managed strings in memory

    Suppose you wrote some C# code like this: var str1 = "ThisIsAString" ; var str2 = "ThisIsAnotherString" ; As you’d expect, each string is stored in the resulting built binary and also in memory when the binary is loaded, resulting in 2 separate strings. Now suppose...
  • Blog Post: Increase the memory available to your tests

        I love having test projects included in my solutions. Software is alive. I’m constantly making improvements/changes/fixes. When I have customers asking for various features in my code, or for code improvements, being agile and able to publish a changed build with utmost confidence relies...
  • Blog Post: Examine your program's available memory: is it leaking?

      Sometimes your program wakes up and finds itself running in an environment that might not be as suitable as you’d like.   For example, it might be distributed to a user running on a machine with not enough memory, and could be failing. Perhaps the failures occur because the user is running...
  • Blog Post: Be careful about nothing in managed code

      Here’s a pattern of code use I’ve seen in a few places. There’s a function DeserializeList that returns an array of various sizes, depending on the input. This code can be called to deserialize (rehydrate) an object from a stream. For example, an XML deserializer might create an XML node from...
  • Blog Post: Dynamically create huge tooltips in WPF TreeView and ListView

      Tooltips are useful. When the mouse hovers over a button a tip can indicate what happens when it’s clicked. The mouse move does not actually invoke the button, but can give information in a passive way.   Sometimes I want to make huge tooltips. This essentially gives more screen real estate...
  • Blog Post: Use colors in WPF to show virtual memory fragmentation

    In this post What is your computer doing with all that memory? Write your own memory browser is sample code that shows how to create a WPF grid view of a memory map of a process. You can click on a column header to sort, and you can see the various loaded images and even a binary display of the actual...
  • Blog Post: The cost of using nothing

    What is the cost of using nothing? Seems like a silly question. Suppose you see code like this: void * MyClass::DoSomething() { int size = sizeof(MyThing) * m_NumItems; var ptr = malloc(size); return ptr; } It’s a method that allocates space for m_NumItems MyThings...
  • Blog Post: See and hear the effects of Garbage Collection

    Sometimes you forget that GC’s occur: it’s hard to see it’s effect, but what does Garbage Collection do to your code? A long time ago (3 decades!) I used Fortran and Assembly code for a PDP-11 16 bit computer to design real time signal processing systems. Two of these were used...
  • Blog Post: Out of memory? Easy ways to increase the memory available to your program

    When you run your VB or C# application, you might get an OutOfMemoryException thrown, even if your machine has lots of memory. Every 32 bit process has a 2^32 bit (4 Gig) address space. That means every pointer has a size of 32 bits (4 bytes) and thus is limited to 4 Billion. That’s...
  • Blog Post: Use Perfmon to analyze your managed memory

    You can learn all sorts of information about your application using Perfmon. You can also inspect various aspects of managed memory. How much time is spent in garbage collection? When managed code runs, memory management is done for you, but at the cost of freezing your application while GC is...
  • Blog Post: Using multiple heaps efficiently

    You can inspect the processes that run on your machine using Task Manager (Ctrl-Shift-Escape) or , Process Explorer from Sysinternals Or you can write your own: here’s source code: What is your computer doing with all that memory? Write your own memory browser (if you’re using Dev10...
  • Blog Post: Managed code using unmanaged memory: HeapCreate, Peek and Poke

    In the old days of Basic (starting over 4 decades ago), there were functions called Peek and Poke that would allow you to read and write memory directly. These were incredibly powerful commands: you could, for example, read and write directly to the hardware, like the video display, the tape cassette...
  • Blog Post: Use Named Pipes and Shared Memory for inter process communication with a child process or two

    I wanted to inject some very low impact code that would run in any “parent” process, like Notepad or Excel or Visual Studio. I wanted to have some User Interface for the data that my injected code gathered about the parent process, and that would work best in a different “child” process, preferably using...
  • Blog Post: Use a Custom Allocator for your STL container

    Last time in Play around with .Net Dictionaries and STL maps , we talked about various containers. Today we’ll write our own allocator for a particular STL container. A general purpose allocator, like malloc in the C Runtime library or HeapAlloc in the Windows API needs to handle allocation requests...
  • Blog Post: Play around with .Net Dictionaries and STL maps

    Last time ( Adventures in interop code: explore string interop memory ) we investigated sharing memory between native and managed code, or between processes. Data structures are very useful: things like arrays, structures, lists, dictionaries, stacks, queues, etc. are used throughout programming...
  • Blog Post: Adventures in interop code: explore string interop memory

    In Create an ActiveX control using ATL that you can use from Fox, Excel, VB6, VB.Net , I showed how to create a control (with which the user can interact) which can be hosted in many places. Today’s sample creates a class in C++ that doesn’t necessarily have UI, and thus isn’t visually hosted,...
  • Blog Post: You can develop code faster

    You can make your Visual Studio experience faster. Often you write some code in Visual Studio, hit F5 to test the code. Repeat. Here’s a simple way to make this experience faster. (works with managed, native, mixed code). When a process is started from the debugger, it is started with...
  • Blog Post: What is your computer doing with all that memory? Write your own memory browser

    What is your computer doing with all that memory? There are various kinds of memory allocated and used in each process. These include: · Managed memory (VB.Net, C#, managed C++) · Heap memory · Stacks · Images (files loaded into a process) VirtualAlloc is the basis of these allocations...
  • Blog Post: Its easy to create a graph of memory use in Excel

    In this post Examine .Net Memory Leaks I showed how to find a .Net managed memory leak. Now let’s create a graph of memory and resource use over time. Start Visual Studio 2008, File->New->Visual Basic (or C#) Windows, WPF application. Dbl click the WPF form to get to the Xaml.cs...
Page 1 of 2 (29 items) 12