I've an application written in VC++ MFC 6.0. Recently got upgraded to .NET 3.5 by compiling in vs2008 and added some WPF applications to it by using managed and unmanaged environment. Basically hosting WPF on win32 window. If I just open a WPF app window, memory keeps going up like 1KB/10 secs. I've tried using .NET Memory profiler & Ants memory profiler. But both are not helping me in detecting the leaks!! I've removed all the WPF controls from the hosted WPF application. It just contains a page having just a frame. But still the leak happens!! Would some one please help me what could cause the application memory go up?
First, you should determine whether you have a managed memory leak or a native memory leak:
Use these PerfMon counters to do that:
Process/Private Bytes,
.NET CLR Memory/# Bytes in All Heaps,
.NET CLR LocksAndThreads/# of current logical Threads.
If 1 is increasing, but 2 remains stable, you have a native memory leak.
If 1 AND 2 are increasing, you have a managed memory leak.
If 3 is increasing unexpectedly, thread stacks are leaking.
If you have found a managed memory leak, .NET memory profiler tools like Ants, YourKit, etc. should help. Since they did not help in your case, you probably have a native leak.
Important: make sure to invoke the garbage collector manually before looking at the memory consumption. If there is not enough memory pressure, the GC will not run and your process' memory increases (which is not a leak in this particular case.) Invoke GC like this:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
This article describes some common causes of memory problems with WPF - may be worth taking a read:
http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/learning-memory-management/WPF-Silverlight-pitfalls
Regarding your attempts to find the leak with memory profilers, try the following with ANTS:
1) Take two snapshots a minute or two apart (the profiler automatically runs a garbage collection prior to taking the snapshot each time).
2) Ensure the baseline snapshot is set to snapshot 1 and the last snapshot is set to snapshot 2.
3) Go to the class list.
4) Under 'Basic filters' select 'From current snapshot show only new objects'.
5) Highlight the largest class then go to the instance list.
6) For one of the instance, open the instance retention graph which shows the chains of reference holding that instance in memory.
7) With a bit of luck, you'll see an object holding onto something it shouldn't which you can then fix. If not, repeat steps 5 & 6 but selecting different classes / instances.
Well, after some soul searching, found out that the leak is actually due to a bug in the framework.
Read this for more
http://social.msdn.microsoft.com/Forums/zh/wpf/thread/5b9ae245-9067-4ca4-b846-180db9f7bde5
Related
I am looking to ensure my app does not consume too much memory on what are still fairly resource constrained devices. Several days ago I was using Instruments to determine how much memory my app was using and I was getting around 4-8 megabytes for Live Bytes. Today I run Instruments again and I am up around 30-35 megabytes for Live Bytes. I don't believe I have made any significant changes to my code between these two times.
My app uses an MKMapView with a custom tile overlay. I put off updating XCode for a fairly long time, and so I suspect that the difference may be that my iOS simulator was still using Google as opposed to Apple maps a few days ago, until I upgraded XCode.
As a small test, I created a new test app that just has an MKMapView, nothing else, and ran Instruments on it. It is common for the Live Bytes of this app to be on the order of 50-90 megabytes, even though it has no custom code whatsoever - I just drag n' dropped the MKMapView on.
Whether this is intentional on Apple's behalf for the new maps to use this much memory I do not know. Perhaps the map tiles are shared across apps and this is fine. Either way though, it complicates coming up with a reasonable approximation for how many Live Bytes I can safely use, given that most earlier suggestions are on the order of 5-20MB, and Apple's MKMapView consumes 50-90MB on its own.
Is there another useful metric I can go by failing Live Bytes being any use now?
Edit: looks like for others this is a legitimate memory management problem and causing app crashes: iOS6 MKMapView using a ton of memory, to the point of crashing the app, anyone else notice this?
I'm facing some problems with my silverlight application.
My application looks like an image manager. You can open some images, edit, view, etc.
My problem is that when you add some images and after remove these images, the memory doesn't return to the same amount used before you add these images. If you repeat these operations (add images and after remove these images), the memory doesn't stop going up.
Why it's happen?
I create a new silverlight 5 project for recreate this scenario, test some codes and try find what is happening.
In the new project I have a StackPanel and 3 buttons with these actions:
1- Add 1000 buttons in a StackPanel;
2- Remove all children of StackPanel;
3- Call GC.Collect;
I call the same actions repeatedly: add(1), remove(2), call GC(3)
After I repeated several times the same action (add and remove). I observed the same result of my first application, the memory doesn't stop going up. Even after I call the GC.Collect the memory is not freed. I don't now what's happen. Is it a possible memory leak of silverlight?
Is your PC 'tablety'?
Silverlight automation kicks in if you have an automation client running on your computer. The most common being:
Tablet PC Input Service (in other words, all 'tablet like' PCs)
Automated Testing tools
Screen Readers (and other accessability software)
This makes silverlight a complete mess and causes a number of bugs, and almost-always makes everything leak like crazy.
I disabled automation by setting this parameter in my html:
<param name="windowless" value="true" />
You can read more here: Silverlight + MVVM + Bindings = Memory leaks?
I am working on a WPF application which doesnot seem to release all the memory when logged out of a screen. On start of app, there is a login screen where I enter userID/Pwd. It takes to a different screen(lets say WPF2). At this point, I am totally unloading the login screen(memory now is 70MB). WHen the WPF2 screen is loaded with dynamically memory goes upto 200MB. When the user logs out from WPF2, login screen loads again. AT this point, I am clearing all objects used in WPF2 in dispose method. But the memory is still 200MB not 70MB, and also when I login again it increases from there.
WHen the application is closed, all memory used is released.
I understand this is very application specific question, but any general ideas are highly appreciated.
Thanks
Items are not released from memory when an event handler is still attached to them, or when the application closes.
This sounds like you have some event handlers to clean up.
Hope this Helps,
You gave not enough information about how you get the memory values. I assume you looked in task manager? If you look only at task manager, you will probably not get the values you expect. Furthermore, memory releasing is not done in the moment a object is no more used. It is an asynchronous task done by the Garbage Collection. If you want to measure memory at a specific point of time, you probably should force GC to free memory before (but only for measurement, not for production code). Look at this thread to get a starting point about the different memory types.
Here you find a very good video, that gives a start to debug memory problems in WPF.
When you call Dispose()on a IDisposableyou can clear umanaged resources.
The GarbageCollector may not have collected WPF2 yet.
I have a standalone WPF application running on .NET 3.5. Every so often, the display simply freezes up for several seconds. This is most noticeable on screens where something is being updated often. Here is a video showing the problem.
While the display is frozen, the interface remains responsive (video).
I've come across some other posts with similar problems who attributed it to a SW/HW rendering issue. To be safe, I disabled HW rendering altogether, but still have the problem.
I ran a file monitor during the freezes to see if there is some extraordinary file access or activity going on, but nothing is out of the ordinary.
Final note: The target platform is a small touch-screen panel PC without much memory or horsepower (512 MB). I only see this issue on the target, never on my development PC, which has much more in the way of resources.
UPDATE
I thought I had fixed the issue by removing some animation code, but it did not work. I am still encountering the problem and I'm at the end of my rope.
Here's some more things I've tried:
Upgraded to .NET 4.0. Same behavior.
Added debug code to all methods that may be invoked via DispatcherTimer (which are called on the UI thread) to make sure none of them are holding up the UI.
I'm really stumped here and have added a bounty. As I mentioned, the problem only occurs on the target PC (link).
I tend to suspect either .NET GC or the OS swapfile when this kind of behavior shows up.
For the former, you could try the .NET performance counters to monitor for suspect activity.
If the device has a swap file, you can disable it and see if the behavior changes.
As others have said, a profiler (or some what of isolating what condition is inducing the delay - even just attaching and breaking the debugger when it occurs) would be a good way to get more information.
Did you tried to profile the application on the tested system? Using a memory and/or performance profiler?
You could get some good informations out of this type of test : some .Net profilers
And here's one for WPF : WPF profiler from microsoft
The culprit was the following method call:
new HwndSource(new HwndSourceParameters());
This was added to my application because it patched a memory leak problem in .NET 3.5. This work-around can be found here. If I remove this call, the rendering issues go away.
I took out the call and fixed the memory leak in another way (removing storyboard animation and using code behind instead)
I did nice winforms 2.0 application and it's working great and customers are still happy but unfortunatelly I cannot solve one issue. The problem is that after using app for a couple of hours, gdi user handles number is rising and rising and finally process cannot allocate more objects and app crashes...
I'm not doing anything fancy, it's regular app, a few forms, a few more modal forms, a few datagridviews and a lot tablelayoutpanels where I'm adding a lot labels and textboxes.
My questions are:
are there any "recommended-practises"
concerning adding/removing regular system
controls on forms at runtime (dgv/tlp)
how to detect system handles'
leaks - preferably using visual
studio and a kind of free plugin
(profiler?)
Detecting graphics and window handle leaks is very difficult. As to a particular strategy for finding them at runtime, I can't suggest anything (though I'd love to hear someone else's!).
As for preventing them, here are a couple of reminders:
While the Control class's finalizer will call Dispose(), this is non-deterministic. You are not guaranteed that ANY object will EVER get finalized by the garbage collector. It's likely that it will, but it's not a guarantee.
In keeping with the above, Forms are an exception. When a Form is shown in a NON-MODAL way (meaning through Show(), NOT ShowDialog()), then when the Form closes it will deterministically call Dispose(). Forms that are shown through ShowDialog() must have Dispose() called manually in order to deterministically clean up the control handle.
Keeping those two things in mind, the most important thing that you can do is to ensure that you always call Dispose() on any object that you explicitly create that implements IDisposable. This INCLUDES Forms, Controls, Graphics objects, even the graphics helper classes like Pen and Brush. All of those classes implement IDisposable, and all of them need to be disposed of as soon as you no longer need them.
Try to cache your graphics utility classes, assuming you're using some. While a Pen and a Brush are fairly lightweight to create, they do take up handles and need to be disposed of when you're finished. Rather than creating them all the time, create a cache manager that allows you to pass in the parameters that you would use in the constructor for those objects and keep that object around. Repeated calls with the same parameters should still only use one single instance. You can then flush your cache on a periodic basis or at specific places in your application if you know where those would be.
Following those guidelines will greatly reduce--if not eliminate--your handle leaks.
I find that using the Task Manager with the GDI Objects column visible essential to finding such leaks. You can target specific areas by breaking before the call, make a note of the GDI objects, then break after the suspect call to determine if the objects are being released properly.
The source code for two useful GDI leak tracking tools can be found here: link text
I have used it successfully on many Visual Studio C++ projects. I am not sure whether I work with .NET as well.