Memory Managment Snippet

Use this board to post your code snippets - tips and tricks
10 posts Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Memory Managment Snippet
Scottie1972
If you notice your application maybe using alot of Memory when it is running, I have a very simple fix for that.

Just add this Class to the very bottom of your "Form1" or what ever or You can create a new one.
Paste this copy to the form
Code: Select all
Public Class Form1
'WHAT EVER YOU HAVE ON FORM!
End Class

Public Class MemoryManagement
    Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" ( _
      ByVal process As IntPtr, _
      ByVal minimumWorkingSetSize As Integer, _
      ByVal maximumWorkingSetSize As Integer) As Integer

    Public Shared Sub FlushMemory()
        GC.Collect()
        GC.WaitForPendingFinalizers()
        If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
            SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
        End If
    End Sub
End Class
Now to use this code just place this code
Code: Select all
MemoryManagement.FlushMemory()
In any event you choose. I use it in the Load Event mostly and when I am parsing a large chunk of data. Like from a Database. via a Button_Click Event or ListView_SelectedIndexChange Event.
But use caution!
You should NOT place this in all of your events or use it for simple events.


OK, so if you open Task Manager while your application is running, find your application in the list. scroll over to the Memory Usage column and see how much memory you are using currently. Then when you fire the Event that you placed "MemoryManagement.FlushMemory()" in you will see the amoiunt of Memory Usage be reduced by half if not more.

Because I like to work with databases and sometimes the data can be quite large. This little snippet works very well when redusing Memory Usage and making the application run slow.


If anyone has another example of a snippet like this, then please share it.
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Memory Managment Snippet
mandai
You shouldn't need to use this if the dispose methods work like they are supposed to.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Memory Managment Snippet
MrAksel
mandai wrote:
You shouldn't need to use this if the dispose methods work like they are supposed to.
Somy types doesn't have a the Dispose method, like a list. A list can use huge memory and don't releases that when you use the Clear method. So this would be useful if you have a list that uses much memory
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Memory Managment Snippet
mandai
By default when a managed list is cleared the memory from the items will be reclaimed over time, not instantly.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: Memory Managment Snippet
Scottie1972
Well, Im not 100% on my understanding of the code completely. But I do notice when I have a large chunck of data that i just loaded from a database into a ListView or a large file loaded into a RichTextBox, After the Load is complete I fire the function
MemoryManagement.FlushMemory()
and the application seems to run much smoother. Plus the memory usage decreases by half if not more... as I dont have alot of memory to start with, it tends to work out for me by using this code. If I dont want to wait for the memory dump to dump on its own than I shouldnt have to.

Like I said. I am not 100% sure about how the code works... all I know is, When the function fires (My List is full) it fires off and seems to free up need memory that the app can use. Therefore, It seems to run smoother to me.
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Re: Memory Managment Snippet
zachman61
Very Nice Scottie definitely going to use this
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Re: Memory Managment Snippet
Skillful
Awesome snip Scottie!! I'll definitely use this!In my Notepad maybe.
Anyways thanks for the snip!
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Memory Managment Snippet
mandai
Using this in a notepad sounds a bit over the top if you ask me.
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Memory Managment Snippet
Axel
mandai wrote:
Using this in a notepad sounds a bit over the top if you ask me.
maybe if the notepads opens files that are like 1gb :P
http://vagex.com/?ref=25000
User avatar
kolega28
VIP - Donator
VIP - Donator
Posts: 338
Joined: Mon Jan 17, 2011 7:12 pm

Re: Memory Managment Snippet
kolega28
I used that in melive now it doesnt crash when registering and logging in
Image
10 posts Page 1 of 1
Return to “Quick Snips”