Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi all,

I'm creating a WPF program for kiosk in full screen running 12 hours/day every day. I have test in my home for memory usage since sometimes it becomes lag and slow. I run the program with task manager on top of the screen and start clicking the function on the screen. I see the memory increase when going to another page.

Every navigation will increase at least 50MB and most consume when next page is displaying a list of images. When timeout arrive and navigating back to home page, the memory still not decrease. I wonder why the memory is increasing.

Does WPF application not release the memory usage after going to another page?

If this still continue for a few hours, the program will crash.

How to encounter this problem actually?

I'm using WPF Application with Page (not WIndow)

Hoping for guide and help

EDITED: MEMORY PROFILER SUMMARY
Profiler Link

What is been used by the bytes(not very sure details)
http://postimg.org/image/7esumzjq5/[^]
Posted
Updated 15-Dec-15 2:41am
v3
Comments
CHill60 14-Dec-15 12:57pm    
We can't see your screen or HDD nor read your mind - we'll need to see some code to help
Dave Kreskowiak 14-Dec-15 13:02pm    
First, do NOT use Task Manager to see how much memory your application is actually using. The .NET CLR RESERVES memory for your application even though your app isn't using it. This is what Task Manager is showing you. Use PerfMon and the .NET Memory counters to see how much memory your app is actually using.
Sergey Alexandrovich Kryukov 14-Dec-15 15:00pm    
5!—SA
Luiey Ichigo 14-Dec-15 21:35pm    
Hi, thanks for recommendation. I see the task manager because pc is hang and when I see the memory use for my application is around 200MB. I'm now try to inspect using memory profiler but I dont know how to read the snapshot. I will update back the image

Hii Luiey Ichigo,
I have also developed POS application and WPF many times consumes much memory even when idle.
Even by following the above links(Provided in Solution 1),sometime a part of memory is not cleared(i.e. memory leak remains).
Please find the code below and use it in some common space like where window is unloaded.
This will remove your much part of Memory but will not clear Memory Leak left by your code(like this will not remove your event handlers if you have not unregistered them but if you open report viewer and call this code after closing report viewer you may find the difference).

Declare this
C#
[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
       internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

       [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
       internal static extern IntPtr GetCurrentProcess();


Consume this code
C#
IntPtr pHandle = GetCurrentProcess();
            SetProcessWorkingSetSize(pHandle, -1, -1);


Please mark this as answer if your prblem is solved.
Thanks!!!
 
Share this answer
 
v2
Comments
Luiey Ichigo 13-Jan-16 22:12pm    
Hi, where should I declare the DLLImport? In App.xaml/Global/New Class/Every page?
Jitesh Hirani 19-Jan-16 4:31am    
It is on you where you need to declare this Code.
Ideally You can Declare in a class and declare the DLL import in it and create a public Method in that class which will have the above consumer.
When ever you Unload a Window you can call this public method of the class and clear memory.
annalisa80 16-Jun-16 8:01am    
Thank you very much! Works for me
sharmayatin489 2-Aug-16 6:19am    
Thanks.
Member 11396276 11-Apr-17 6:13am    
Thank you very much ;-)
Memory Usage of the application is determined by our coding practices. Without seeing your code, we can't find out the reason for your issue. Anyway, following best programming practices will help you to develop memory efficient applications. Refer the following CP article,
1. Building High Performance WPF/E Enterprise Class Application in C# 4.5[^]
2. WPF / MVVM Real-Time Trading Application[^]
 
Share this answer
 
Comments
Luiey Ichigo 14-Dec-15 22:56pm    
I want to post my code but which part, that I dont know where to start. Since every navigation to other page will increase the memory usage. When navigating back to home screen, its still stay at last memory usage. If navigate again, then it will increase more
VR Karthikeyan 15-Dec-15 1:35am    
In this case, please refer the above specified articles, and use those best practices in your code.
johannesnestler 15-Dec-15 10:56am    
no answer but chances are high you do something wrong: somethings come to my mind you can look for:
* Did you use strong (normal) references where you should have used WeakReference?
* Do you subscribe to handle events of long living objects (the Provider will hold a reference to every subscriber) wich then can't be GCed (may be solved by unsubscribing if handler is not needed any more)
* Do you load other resources on each Navigation which are then refernced by other parts of the System (maybe a static "global" property or something)
* Do you use any unmanaged "things" which may leak memory?
Luiey Ichigo 22-Dec-15 21:23pm    
Hi johan,
1) What mean by strong/weak reference?
2) Every event is using within single page only. Not sharing and be used by another page. When the page is unloaded, I will empty every variable that been declared and use from the page e.g. Screen Timer, BackgroundWorker, Byte().
3) I load every DynamicResource from ResourceDictionary page. All page have same button/control for some function e.g. Continue, Back, Next, Previous.
4) On timer timeout, the page will navigate back to home page. At new instance of the page, I will clear all global variable/declaration e.g Structure, Arraylist.
OK, so you've got about 273MB worth of stuff sitting in byte arrays. What are they and what are you doing with them?

We can't see you're code so it's impossible to tell you what's going on.
 
Share this answer
 
Comments
Luiey Ichigo 15-Dec-15 0:09am    
The byte that im holding contains image of each ID. I will store the ID and image to arraylist and stream each byte to imagebrush on program. Is it mean that after I'm navigating to other page, the arraylist is not release as nothing?
Dave Kreskowiak 15-Dec-15 15:28pm    
I can't see your code so it's impossible to say. You should really understand variable scope and the debugger to figure out what's going on.

If it's climbing every time you leave a page and go back, I'd say that you're adding/duplicating data into these structures when you don't have to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900