Posted: Wed Sep 28, 2016 8:00 pm Post subject: Vb.Net (Flushing/Clearing) Ram & CPU Source Code
Original Source By: AfaoMax (Improvised by: Me RecycledTwice)
this will help your program drastically, as far as ram resources and cpu usage.
x1 - Timer (set to enabled, tick set to 1)
Code:
Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal process As IntPtr, ByVal minimumWorkingSetSize As Integer, ByVal maximumWorkingSetSize As Integer) As Integer
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Public Sub FlushMemory()
Try
GC.Collect(1, GCCollectionMode.Optimized) 'The Bit in parenthesis Was added by me
GC.GetTotalMemory(False)
GC.WaitForPendingFinalizers()
If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
Posted: Wed Sep 28, 2016 9:48 pm Post subject:
You shouldn't be calling GC functions yourself unless absolutely necessary. And if your application is at the point where it becomes needed, then you need to go back and review your code because you are either doing something extremely wrong, or very poorly. Usually the only time you should ever manually call GC functions are to block it from collecting an object that you want to persist.
You are also causing two garbage collections to happen in a row (by calling GetTotalMemory another GC is done) and you do not use the return. That is just useless overhead.
Something else to consider is that Timers are not guaranteed to be threaded / threadsafe. Meaning your GC timer can land up locking up the UI thread in some conditions.
Overall things like this should not be needed. If you are having memory issues with your apps then you need to review your code and do things better. _________________
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum