Memory Managment Snippet
Use this board to post your code snippets - tips and tricks
10 posts
Page 1 of 1
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
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.
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
Now to use this code just place this code
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
Code: Select all
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.MemoryManagement.FlushMemory()
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.
You shouldn't need to use this if the dispose methods work like they are supposed to.
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!
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!
By default when a managed list is cleared the memory from the items will be reclaimed over time, not instantly.
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.
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.
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
Awesome snip Scottie!! I'll definitely use this!In my Notepad maybe.
Anyways thanks for the snip!
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!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
Using this in a notepad sounds a bit over the top if you ask me.
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
I used that in melive now it doesnt crash when registering and logging in
10 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023