Windows 12 Leaks

0 views
Skip to first unread message

Deidamia Bassiti

unread,
Aug 3, 2024, 6:13:02 PM8/3/24
to exheconkert

Before installing my windows service in production, I was looking for reliable tests that I can perform to make sure my code doesn't contain memory leaks.However, All what I can find on the net was using task manager to look at used memory or some paid memory profiler tools.

Leaking references means you forget to set object references to null, or they never leave scope, and this is almost as likely to occur in Garbage collected languages as not - lists building up and not clearing, event handlers pointing to delegates, etc.

It's the GC equivalent of memory leaks and has the same result. This program tells you what references are taking up tons of memory - and you will know if it's supposed to be that way or not, and if not, you can go find them and fix the problem!

NOTE: You will likely have to run your app not as a service to use this. It starts first and then runs your app. You can do this with TopShelf or by just putting the guts in a dll that runs from an EXE that implments the service integrations (service host pattern).

Although managed code implies no direct memory management, you still have to manage your instances. Those instances 'claim' memory. And it is all about the usage of these instances, keeping them alive when you don't expect them to be.

Just one of many examples: wrong usage of disposable classes can result in a lot of instances claiming memory. For a windows service, a slow but steady increase of instances can eventually result in to much memory usage.

It is great to analyze memory leaks during development. It uses the concept of snapshots to compare new instances, disposed instances etc. This is a great help to understand how your service uses its memory. You can then dig deeper into why new instances get created or are kept alive.

Yes, you can test to confirm whether memory leaks are introduced.However, just out-of-the box this will not be very useful. This is because no one can anticipate what will happen during runtime. The tool can analyze your app for common issues, but this is not guaranteed.

Of course a memory profiler is the first kind of tool to try, but it will only tell you whether your instances keep increasing. You still want to know whether it is normal that they are increasing. Also, once you have established that some instances keep increasing for no good reason, (meaning, you have a leak,) you will want to know precisely which call trees lead to their allocation, so that you can troubleshoot the code that allocates them and fix it so that it does eventually release them.

Get in the habit of explicitly undoing everything that you do at the end of the scope of that thing which you are doing. For example, if you register an observer to the event of some observee, there should should always be some point in time (the disposal of the observer or the observee?) that you de-register it. In theory, garbage collection should take care of that by collecting the entire graph of interconnected observers and observees, but in practice, if you don't kick the habit of forgetting to undo things that you do, you get memory leaks.

Use IDisposable as much as possible, and make your destructors report if someone forgot to invoke Dispose(). More about this method here: Mandatory disposal vs. the "Dispose-disposing" abomination Disclosure: I am the author of that article.

Have regular checkpoints in your program where you release everything that should be releasable (as if the program is performing an orderly shutdown in order to terminate) and then force a garbage collection to see whether you have any leaks.

If instances of some class appear to be leaking, use the following trick to discover the precise calling tree that caused their allocation: within the constructor of that class, allocate an exception object without throwing it, obtain the stack trace of the exception, and store it. If you discover later that this object has been leaked, you have the necessary stack trace. Just don't do this with too many objects, because allocating an exception and obtaining the stack trace from it is ridiculously slow, only Microsoft knows why.

I do not agree that you can trust the Task Manager to check if you have a memory leak or not. The problem with a garbage collector is that it can decide based on heuristics to keep the memory after a memory spike and do not return it to the OS. You might have a 2 GB Commit size but 90% of them can be free.

You should use VMMAP to check during the tests what type of memory your process contains. You do not only have the managed heap, but also unmanaged heap, private bytes, stacks (thread leaks), shared files and much more which need to be tracked.

VMMap has also command line interface which makes it possible to create snapshots at regular intervals which you can examine later. If you have a memory growth you can find out which type of memory is leaked which needs depending on the leak type different debugging tooling approaches.

I would not say that the Garbage collector is infallible. There are times when it fails unknowingly and they are not so straight forward. Memory streams are a common cause of memory leaks. You can open them in one context and they may never even get closed, even though the usage is wrapped in a using statement (the definition of a disposable object that should be cleaned up immediately after its usage falls out of scope). If you are experiencing crashes due to running out of memory, Windows does create dump files that you can sift through.

Also, you want to turn off automatic tracking on Entity Framework where you are only reading and not writing. Best to isolate your writes, use a using() at this point, get a dbContext (with tracking on), write your data.

If you want to investigate what is on the heap. The best tool I've used is RedGate ANTS -gate.com/products/dotnet-development/ants-memory-profiler/solving-memory-problems/getting-started not cheap but it works.

However, by using the using() pattern where-ever you can (don't make a static or singleton DbContext and never have one context in a massive loop of updates, dispose of them as often as you can!) then you find memory isn't often an issue.

Unless you're dealing with unmanaged code, i would be so bold to say you don't have to worry about memory leaks. Any unreferenced object in managed code will be removed by the garbage collector, and the possibility in finding a memory leak within the .net framework i would say you should be considered very lucky (well, unlucky). You don't have to worry about memory leak.

However, you can still encounter ever-growing memory usage, if references to objects are never released. For example, say you keep an internal log structure, and you just keep adding entries to a log list. Then every entry still have references from the log list and therefore will never be collected.

From my experience, you can definitely use the task manager as an indicator whether your system has growing issues; if the memory usage steadily keep rising, you know you have an issue. If it grows to a point but eventually converges to a certain size, it indicates it has reached its operating threshold.

If you want a more detailed view of managed memory usage, you can download the process explorer here, developed by Microsoft. It is still quite blunt, but it gives a somewhat better statistical view than task manager.

The majority of window leaks are caused by a breakdown in the chemical components of the sealant. The telltale sign of degraded sealant is condensation found around the edge of the window when it rains. You can also look for discoloration or abrasions on the sealant itself.

In some cases, problems with your home can be misdiagnosed as a leaking window, including missing shingles, cracks in your walls, and even clogged gutters. These issues can direct cold air or excess moisture into your home. Sometimes it pays to investigate other potential problems before repairing or replacing your window.

One immediate trick to help pinpoint the cause of your leak is to spray a hose along the edges of your windows. After it is thoroughly tested, look for areas where the moisture is coming through. From there, you may be able to identify home repairs that you or a professional can handle, such as clogged gutters redirecting water or missing shingles on your roof.

If your window leak is caused by damaged sealant, you may be able to repair it yourself. First, choose a dry day to strip the sealant from the edges of your window. Once it is all clear, dry, and cleaned, reapply in even, consistent strokes.

The most comprehensive and reliable way to fix a leaking window is to replace it completely. When installed by an experienced professional, quality windows can protect your home from moisture and help lower your heating and cooling bills.

Leaking windows are inconvenient because they let the water that is supposed to remain on the outside into places it shouldn't be. Oh, its aggravating enough that you might have to try to sleep on a wet mattress, but that's hardly the least of your problems when you have a leaky boat. The real kicker is when those leaks cause a lot of damage, as they are wont to do, that cost you a whole lot of money to repair, and even more money when you go to sell the boat if you don't.

I just saw a good example of that this morning when I surveyed a 10 year old Chris Craft that had about $5,000 worth of interior damage because of just plain lousy design of the windows that caused leaks that couldn't be fixed. This after the survey of a 47 foot Albin trawler last week that had an all wood paneled interior that had about $8,000 worth of water damage, preceded by a 29 foot Formula with an interior that was completely ruined by window leaks, for which the cost to repair would have been well over $10,000. And a week before that I did a 42' sailboat with leaking chain plates that rotted out the bulkheads that they were attached to, causing damage worth at least $15,000. So right there in just two weeks were three boats with water damage estimated at over $38,000, and that does not include the cost of repairing the leaky windows and design defects.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages