. Net program memory usage problems. Net program memory usage Problems

Source: Internet
Author: User
. Net program memory usage Problems

1. Use the Performance Testing Tool dottrace 3.0 to calculate that the code in your program occupies a large amount of memory.

2. Forced garbage collection

3. Multiple dispose and close

4. Call timer every several seconds: setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1). For details, see the appendix.

5. Select release for release.

6. Note that less garbage is generated during code writing. For example, string + string will generate a large amount of garbage. You can use stringbuffer. append

7. This. Dispose (); this. Dispose (true); this. Close (); GC. Collect ();

8. Pay attention to the scope of variables. Specifically, if a variable is only temporarily used, it should not be defined as a member variable. GC recycles resources based on the relational network.

9. Check whether memory leakage exists. For details, refer to: Memory leakage Baidu encyclopedia.

 

Periodically clear and execute the garbage collection code:

// Use a timer in the program to call the function every few seconds and open the task manager. You will be surprised to find that

# Region memory recovery

[Dllimport ("kernel32.dll", entrypoint = "setprocessworkingsetsize")]

Public static extern int setprocessworkingsetsize (intptr process, int minsize, int maxsize );

/// <Summary>

/// Release the memory

/// </Summary>

Public static void clearmemory ()

{

GC. Collect ();

GC. waitforpendingfinalizers ();

If (environment. osversion. Platform = platformid. win32nt)

{

App. setprocessworkingsetsize (system. Diagnostics. process. getcurrentprocess (). Handle,-1,-1 );

}

}

# Endregion

 

Setprocessworkingsetsize function scam

 

Physical memory and virtual memory

Physical memory, as the name implies in applications, is physically the size of the memory actually inserted into the board. It is the size of the physical memory when you look at the machine configuration.

If the number of programs executed is large or large, the physical memory will be exhausted. to solve this problem, the virtual memory technology is used in windows, that is, some hard disk space is used as the memory. When the memory is used up, the computer will automatically call the hard disk to act as the memory, to ease memory shortage.

Virtual Memory is inevitably used for a program. Because the code that is not executed frequently or has not been executed for a long time does not need to be left in the physical memory, it will only cause waste, when you execute this part of the code, call it again. the Windows Task Manager can help us see the virtual memory of the process. call up the task manager, click "View"-"Select column" in the menu, and hook the "virtual memory size" in the displayed window.

How much virtual memory should a program use? Not necessarily, but it should be best to match the virtual memory properly. the following will expose the scheme that seems to have called a large number of pictures and programs running a large number of databases. Why does it "Occupy" less than 1 MB of memory.

It turns out to be the setprocessworkingsetsize function.

Msdn's description of this function: This function is used to set the minimum and maximum runtime space of the application, and only the memory needed is retained. when the application is idle or the system memory is too low, the operating system will automatically call this mechanism to set the application memory. applications can also use virtuallock to lock a certain range of memory from being released by the system. When you increase the runtime space for the application, the physical memory you can obtain depends on the system, this may cause performance degradation for other applications or overall system performance reduction. This may also cause requests to physical memory operations to fail, such as creating processes, threads, and kernel pools, you must use this function with caution.

That is to say, this function does not save memory, but forces the physical memory of the process to be moved to the virtual memory.

In other materials, this function "May cause page-missing interruptions, seriously affecting performance ". function prototype: bool setprocessworkingsetsize (handle hprocess, size_t dwminimumworkingsetsize, size_t dwmaximumworkingsetsize); in this simple example, the program occupies 300 kb of memory.

Create a standard VB project, place a timer1 in form1, set the interval attribute to 1000 (1 second), and enter the following code in the code editing box:

Private declare function setprocessworkingsetsize lib "Kernel32" (byval hprocess as long, byval dwminimumworkingsetsize as long, byval dwmaximumworkingsetsize as long) As long private declare function getcurrentprocess "Kernel32 "() as long private sub timer1_timer () setprocessworkingsetsize getcurrentprocess (), 50000,100 000 end sub, generate project 1.exe, execute, call up the task manager to view, and find that the memory usage is only 320 KB. if the timer is disabled, the memory of the process is about 4 MB. you must regularly execute this function. Otherwise, the virtual memory will be slowly tuned to restore the original memory size. if you want to reduce a program that originally occupies a large amount of memory to several hundred kb, use the same method.

Dangers of deception

It is very useful if the setprocessworkingsetsize function is used normally. however, in order to fool the user's eyes, a large amount of memory is compressed into the virtual memory every second or even dozens of milliseconds, which brings unpredictable harm. let's take a look at what this article says: "This is because it only temporarily moves the memory occupied by the application to the virtual memory. Once the application is activated or has operation requests, the memory will be occupied again. if you use this method to set the memory occupied by the program, the system performance may be reduced to some extent because the system needs to frequently perform page exchanges between the memory and the hard disk. ".

Yes, if you use this kind of software, it means that your hard disk will put a lot of I/O data every second; the hard disk's magnetic needle will rotate hard... (Of course, the hard disk magnetic needle cannot be rotated ^ _ ^, but the choice is more powerful ).

Doesn't BT hurt the memory? Otherwise, most BT software now has cache technology. looking at bitcomet's official explanation of the cache technology: "the hard disk will sound very loud during the traditional BT High-speed download, which is caused by a large number of random reads .... bitcomet allows users to set the cache size .... it can be clearly seen that the hard disk protection is achieved by sacrificing a small amount of memory for caching."

Is there a cold feeling? One type of software would rather sacrifice the memory and reduce the protection of hard disks. The other type of software, however, is designed to fool users and make CPU and hard disks run more quickly ......

Catch a murderer

There are a lot of such software. I used one of the desktop tools as an example to expose its mask (no name left ). after running the software, perform any operations, open the Process Manager, call the virtual memory column, and find the process. 3:

OK, 20 mb virtual memory, but only 632 kb physical memory. if you are careful, you will find that the row is flickering every one second. That is the result of calling setprocessworkingsetsize every second. in addition, we open the Norton process viewer to view the CPU usage of the Process, 4:

We can see that even if the software is not operated, there are 3% CPU usage fluctuations per second (although this does not indicate anything ). in addition, the memory box shows the physical memory and virtual memory usage, which are far from each other. in addition, the hook api technology can be used to prove the behavior of calling setprocessworkingsetsize every second.

What should I do?

In this article, we only want to let users know the actual usage of software resources. The programmer should take the effort to reduce the memory consumption from the code, instead of ignoring the user. Call setprocessworkingsetsize

It will bring some benefits, but when to call and how to call should meet two requirements: 1. When the program is temporarily unavailable (for example, minimized); 2, the physical memory and virtual memory should be in a suitable proportion (instead of 600 KB or 20 mb); 3, or do not call, let Windows process.

Transfer http://www.cnblogs.com/fromchaos/archive/2012/08/27/2658033.html

1. Use the Performance Testing Tool dottrace 3.0 to calculate that the code in your program occupies a large amount of memory.

2. Forced garbage collection

3. Multiple dispose and close

4. Call timer every several seconds: setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1). For details, see the appendix.

5. Select release for release.

6. Note that less garbage is generated during code writing. For example, string + string will generate a large amount of garbage. You can use stringbuffer. append

7. This. Dispose (); this. Dispose (true); this. Close (); GC. Collect ();

8. Pay attention to the scope of variables. Specifically, if a variable is only temporarily used, it should not be defined as a member variable. GC recycles resources based on the relational network.

9. Check whether memory leakage exists. For details, refer to: Memory leakage Baidu encyclopedia.

 

Periodically clear and execute the garbage collection code:

// Use a timer in the program to call the function every few seconds and open the task manager. You will be surprised to find that

# Region memory recovery

[Dllimport ("kernel32.dll", entrypoint = "setprocessworkingsetsize")]

Public static extern int setprocessworkingsetsize (intptr process, int minsize, int maxsize );

/// <Summary>

/// Release the memory

/// </Summary>

Public static void clearmemory ()

{

GC. Collect ();

GC. waitforpendingfinalizers ();

If (environment. osversion. Platform = platformid. win32nt)

{

App. setprocessworkingsetsize (system. Diagnostics. process. getcurrentprocess (). Handle,-1,-1 );

}

}

# Endregion

 

Setprocessworkingsetsize function scam

 

Physical memory and virtual memory

Physical memory, as the name implies in applications, is physically the size of the memory actually inserted into the board. It is the size of the physical memory when you look at the machine configuration.

If the number of programs executed is large or large, the physical memory will be exhausted. to solve this problem, the virtual memory technology is used in windows, that is, some hard disk space is used as the memory. When the memory is used up, the computer will automatically call the hard disk to act as the memory, to ease memory shortage.

Virtual Memory is inevitably used for a program. Because the code that is not executed frequently or has not been executed for a long time does not need to be left in the physical memory, it will only cause waste, when you execute this part of the code, call it again. the Windows Task Manager can help us see the virtual memory of the process. call up the task manager, click "View"-"Select column" in the menu, and hook the "virtual memory size" in the displayed window.

How much virtual memory should a program use? Not necessarily, but it should be best to match the virtual memory properly. the following will expose the scheme that seems to have called a large number of pictures and programs running a large number of databases. Why does it "Occupy" less than 1 MB of memory.

It turns out to be the setprocessworkingsetsize function.

Msdn's description of this function: This function is used to set the minimum and maximum runtime space of the application, and only the memory needed is retained. when the application is idle or the system memory is too low, the operating system will automatically call this mechanism to set the application memory. applications can also use virtuallock to lock a certain range of memory from being released by the system. When you increase the runtime space for the application, the physical memory you can obtain depends on the system, this may cause performance degradation for other applications or overall system performance reduction. This may also cause requests to physical memory operations to fail, such as creating processes, threads, and kernel pools, you must use this function with caution.

That is to say, this function does not save memory, but forces the physical memory of the process to be moved to the virtual memory.

In other materials, this function "May cause page-missing interruptions, seriously affecting performance ". function prototype: bool setprocessworkingsetsize (handle hprocess, size_t dwminimumworkingsetsize, size_t dwmaximumworkingsetsize); in this simple example, the program occupies 300 kb of memory.

Create a standard VB project, place a timer1 in form1, set the interval attribute to 1000 (1 second), and enter the following code in the code editing box:

Private declare function setprocessworkingsetsize lib "Kernel32" (byval hprocess as long, byval dwminimumworkingsetsize as long, byval dwmaximumworkingsetsize as long) As long private declare function getcurrentprocess "Kernel32 "() as long private sub timer1_timer () setprocessworkingsetsize getcurrentprocess (), 50000,100 000 end sub, generate project 1.exe, execute, call up the task manager to view, and find that the memory usage is only 320 KB. if the timer is disabled, the memory of the process is about 4 MB. you must regularly execute this function. Otherwise, the virtual memory will be slowly tuned to restore the original memory size. if you want to reduce a program that originally occupies a large amount of memory to several hundred kb, use the same method.

Dangers of deception

It is very useful if the setprocessworkingsetsize function is used normally. however, in order to fool the user's eyes, a large amount of memory is compressed into the virtual memory every second or even dozens of milliseconds, which brings unpredictable harm. let's take a look at what this article says: "This is because it only temporarily moves the memory occupied by the application to the virtual memory. Once the application is activated or has operation requests, the memory will be occupied again. if you use this method to set the memory occupied by the program, the system performance may be reduced to some extent because the system needs to frequently perform page exchanges between the memory and the hard disk. ".

Yes, if you use this kind of software, it means that your hard disk will put a lot of I/O data every second; the hard disk's magnetic needle will rotate hard... (Of course, the hard disk magnetic needle cannot be rotated ^ _ ^, but the choice is more powerful ).

Doesn't BT hurt the memory? Otherwise, most BT software now has cache technology. looking at bitcomet's official explanation of the cache technology: "the hard disk will sound very loud during the traditional BT High-speed download, which is caused by a large number of random reads .... bitcomet allows users to set the cache size .... it can be clearly seen that the hard disk protection is achieved by sacrificing a small amount of memory for caching."

Is there a cold feeling? One type of software would rather sacrifice the memory and reduce the protection of hard disks. The other type of software, however, is designed to fool users and make CPU and hard disks run more quickly ......

Catch a murderer

There are a lot of such software. I used one of the desktop tools as an example to expose its mask (no name left ). after running the software, perform any operations, open the Process Manager, call the virtual memory column, and find the process. 3:

OK, 20 mb virtual memory, but only 632 kb physical memory. if you are careful, you will find that the row is flickering every one second. That is the result of calling setprocessworkingsetsize every second. in addition, we open the Norton process viewer to view the CPU usage of the Process, 4:

We can see that even if the software is not operated, there are 3% CPU usage fluctuations per second (although this does not indicate anything ). in addition, the memory box shows the physical memory and virtual memory usage, which are far from each other. in addition, the hook api technology can be used to prove the behavior of calling setprocessworkingsetsize every second.

What should I do?

In this article, we only want to let users know the actual usage of software resources. The programmer should take the effort to reduce the memory consumption from the code, instead of ignoring the user. Call setprocessworkingsetsize

It will bring some benefits, but when to call and how to call should meet two requirements: 1. When the program is temporarily unavailable (for example, minimized); 2, the physical memory and virtual memory should be in a suitable proportion (instead of 600 KB or 20 mb); 3, or do not call, let Windows process.

Transfer http://www.cnblogs.com/fromchaos/archive/2012/08/27/2658033.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.