Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The solution of VB.NET memory occupation

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the solution to the memory occupation of VB.NET, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

VB.NET brings us many new ways of application and coding, which greatly improves the programming efficiency of programmers to a certain extent. And its application diversification, flexibility and other characteristics greatly attract developers.

VB.NET memory usage method 1: hide the form or minimize startup

The following VB.NET 2005 code snippet controls whether the form is minimized and hidden through the form variable m_IsHide, initialized to True.

In order to run the following code, you also need a NotifyIcon and an associated ContextMenuStrip, which contains two menus mnuShow and mnuHide.

Private m_IsHide As Boolean = True

Private Sub frmMain_Activated () Sub

FrmMain_Activated (ByVal sender As

Object, ByVal e As System.EventArgs)

Handles Me.Activated

If m_IsHide Then

Me.WindowState = FormWindowState.

Minimized

Me.Hide ()

Else

Me.WindowState = FormWindowState.

Normal

Me.Show ()

End If

End Sub

Private Sub frmMain_Resize () Sub

FrmMain_Resize (ByVal sender As Object

ByVal e As System.EventArgs)

Handles Me.Resize

If Me.WindowState = FormWindowState.

Minimized Then

M_IsHide = True

Me.Hide ()

End If

End Sub

Private Sub mnuShow_Click () Sub

MnuShow_Click (ByVal sender As

System.Object, ByVal e As System.

EventArgs) Handles mnuShow.Click

M_IsHide = False

Me.WindowState = FormWindowState.Normal

Me.Show ()

End Sub

Private Sub mnuHide_Click () Sub

MnuHide_Click (ByVal sender As System.

Object, ByVal e As System.EventArgs)

Handles mnuHide.Click

M_IsHide = True

Me.WindowState = FormWindowState.Minimized

Me.Hide ()

End Sub

The functions of the above code are:

Sample Code interpretation of VB.NET executable statement

Detailed explanation of the steps related to the creation process of VB.NET

How to operate the VB.NET stream correctly

Interpretation of two ways of sending E-mail by VB.NET

Discussion on the example of VB.NET calling WinAPI

1. Minimize and hide at startup

2. Click mnuHide form to hide

3. Click the mnuShow form to restore.

4. The form is hidden when minimized

In a word, it is programming to minimize the startup of the form.

VB.NET memory occupation method 2: Windows pours the memory occupied by the program into virtual memory, which can create the illusion of small memory occupation.

There is a complete introduction to this in Rakuten's article "moving the occupation of physical memory to Virtual memory-revealing Secrets": "but the operating system itself has this mechanism, that is, when the program is not in use (minimized), the operating system will call certain commands to move the memory occupied by the program to virtual memory, leaving only a small portion of the regular code."

BOOL SetProcessWorkingSetSize (HANDLE hProcess, SIZE_T dwMinimumWorkingSetSize, SIZE_T dwMaximumWorkingSetSize)

"setting the 2 SIZE_T parameters to-1 allows the memory used by the process to be swapped into virtual memory, leaving only a small portion of the code.

In fact, using this function does not improve performance, nor does it really save memory. Because he only temporarily moves the memory occupied by the application to virtual memory, it will be reoccupied once the application is activated or when there is an operation request. If you force the use of this method to set the memory used by the program, it may degrade the system performance to some extent, because the system needs to exchange pages between memory and hard drives frequently. "

Give an example of my use in vb2005:

Imports System.Runtime.InteropServices

Module modMain Module modMain

< DllImport("kernel32.dll")>

_

Public Function SetProcessWorkingSetSize ()

Function SetProcessWorkingSetSize (_

ByVal hProcess As Integer, _

ByVal dwMinimumWorkingSetSize As Integer, _

ByVal dwMaximumWorkingSetSize As Integer) _

As Integer

End Function

< DllImport("kernel32.dll")>

_

Public Function GetCurrentProcess ()

Function GetCurrentProcess () As Integer

End Function

Public Sub ReduceMem () Sub ReduceMem ()

SetProcessWorkingSetSize (GetCurrentProcess-1,-1)

End Sub

End Module

The effect of using the ReduceMem () function is great, but it is not recommended to use it often. I just use it when I use FormLoad, and the effect is very good.

It is worth mentioning that if you do not use the GetCurrentProcess () function to get the current Process, but instead use Process.GetCurrentProcess (), you will not be able to effectively reduce memory usage.

The above is the solution to VB.NET memory consumption. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report