none
using kernel32.dll to release Memoryusgae, is this good approach? RRS feed

  • Question

  • Hi,

    I ma using Avalon Dock in my WPF Application, but when i close my Tab my memory did not remove so i am using kernel32.dll, so far i have not seen any problem, but i have impression kernel not good if doenot use properly, so checking here, below is line let me know :

       [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize",
    ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
            private static extern int SetProcessWorkingSetSize(
              IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);

    Thanks

    A-


    Ashok

    Tuesday, February 26, 2013 6:31 PM

Answers

  • Hi Ashok,

    Welcome to the MSDN Forum.

    Set the tab variable to null, and then call Collect.

    	   MyFinalizeObject mfo = null;
    
    	   // Create and release a large number of objects 
    	   // that require finalization. 
    	   for(int j = 0; j < numMfos; j++)
    	   {
    	      mfo = new MyFinalizeObject();
    	   }
    
    	   //Release the last object created in the loop.
    	   mfo = null;
    
    	   //Force garbage collection.
    	   GC.Collect();
    
    	   // Wait for all finalizers to complete before continuing. 
    	   // Without this call to GC.WaitForPendingFinalizers,  
    	   // the worker loop below might execute at the same time  
    	   // as the finalizers. 
    	   // With this call, the worker loop executes only after 
    	   // all finalizers have been called.
    	   GC.WaitForPendingFinalizers();

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 28, 2013 9:55 AM
    Moderator
  • There are several articles about this thematic. TaskManager does NOT give an accurate value. Just google ".NET Taskmanager memory" and you get articles like this that explain teh issue:

    http://www.itwriting.com/dotnetmem.php


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Jumpingboy Friday, March 15, 2013 5:27 PM
    Tuesday, March 5, 2013 4:38 PM

All replies

  • Befoer you even think of optimising memory, read how the Garbage Collector works:
    http://msdn.microsoft.com/en-us/library/vstudio/0xy59wtx.aspx
    Because that is the code that actually assigns and frees memory.

    Among other things, it may not run (at all) if no more memory is requested. The only way to leak memory in managed code is the "I totally forgot I still have a reference" leak. So check and tripple check if you might reference the AvalonTab or any component off it somewhere outside the AvalonTab.

    A hard to find leak is:

    Button btn = new Button();
    btn.ClickEvent += this.btn_Click;
    this.content.add(btn);

    This is a simple cross Referencing: the tab/window references the Button (part of it's content), the button References a Event (a colelction of delegates), the collection of delegates references the tab/window.

    This code makes no leak. If however anywhere else than the tab/window itself there is a reference on the button or the event the tab, all it's elements, the button and the event will be held in memory!


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


    Tuesday, February 26, 2013 11:43 PM
  • you can call garbage collector manually if needed as below

    GC.Collect();
    GC.WaitForPendingFinalizers();

    Andy
    Wednesday, February 27, 2013 12:58 AM
  • Thanks for reply.

    i tried most of "Garbage Collection" setting but did not work for me.

    as you said if i open my Contentpanel like u said above no memory leak? , but i do see

    in my task manager , when i open any tab then Memory usage ll go up and when i close it wont letting down.

    even i tried to use "Garbage Collection" on tab close, even i tried this one:

    GC.Collect();
    GC.WaitForPendingFinalizers();

    so what you think Task Manager Memory usage is not memory leak thing, when i close Application then memory usage gone, but closing tab not getting down, so by this if i open same tab 4* 5 times my computer memory usage is very high.

    Thanks

    A-




    Ashok

    Wednesday, February 27, 2013 2:33 PM
  • Thanks for reply.

    i tried most of "Garbage Collection" setting but did not work for me.

    as you said if i open my Contentpanel like u said above no memory leak? , but i do see

    in my task manager , when i open any tab then Memory usage ll go up and when i close it wont letting down.

    The TaskManager is not a valid way to measure memory usage. There is actually no 100% reliable way to get the memory usage, only close aproximations:

    http://stackoverflow.com/questions/2971594/measuring-memory-usage-in-net

    http://stackoverflow.com/questions/4788358/how-to-determine-memory-usage-in-my-net-application

    But I can only repeat it again:
    As long as you removed all References to something in your Code, there is no way to leak memory in Managed Code. The GC makes that outright impossible.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Wednesday, February 27, 2013 2:51 PM
  • Thanks Christopher84,

    so what is Task Manager Mem Usgae , if this is not memory leak,

    usually client asked why its using this much or not , and every time blaming memory leak by our application.

    I am not good into in memory leak, so pls explain.

    Thanks

    A-


    Ashok

    Wednesday, February 27, 2013 6:34 PM
  • Hi Ashok,

    Welcome to the MSDN Forum.

    Set the tab variable to null, and then call Collect.

    	   MyFinalizeObject mfo = null;
    
    	   // Create and release a large number of objects 
    	   // that require finalization. 
    	   for(int j = 0; j < numMfos; j++)
    	   {
    	      mfo = new MyFinalizeObject();
    	   }
    
    	   //Release the last object created in the loop.
    	   mfo = null;
    
    	   //Force garbage collection.
    	   GC.Collect();
    
    	   // Wait for all finalizers to complete before continuing. 
    	   // Without this call to GC.WaitForPendingFinalizers,  
    	   // the worker loop below might execute at the same time  
    	   // as the finalizers. 
    	   // With this call, the worker loop executes only after 
    	   // all finalizers have been called.
    	   GC.WaitForPendingFinalizers();

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 28, 2013 9:55 AM
    Moderator
  • Hi Ashok,

    Does this work for you?

    I have marked this thread temporarily.

    If it doesn't work for you, please feel free to follow up.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 5, 2013 2:09 PM
    Moderator
  • There are several articles about this thematic. TaskManager does NOT give an accurate value. Just google ".NET Taskmanager memory" and you get articles like this that explain teh issue:

    http://www.itwriting.com/dotnetmem.php


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Jumpingboy Friday, March 15, 2013 5:27 PM
    Tuesday, March 5, 2013 4:38 PM