C # Thread does not free memory

I have a windows service written in C # .Net. When the service starts, I create a new thread, as shown below.

new Thread(new ThreadStart(Function1)).Start(); 

This stream is infinitely infinite and fulfills the duties expected of my service. Once a day, I need to simultaneously perform another operation for which my thread generates a second thread, as shown below

 new Thread(new ThreadStart(Function2)).Start(); 

This second thread performs a very simple function. It reads all lines of a text file using FileReadAllLines, quickly processes this information, and exits.

My problem is that the memory used by the second thread that reads the file is not collected. I will let my service work for 3 hours, hoping that the GC will be called, but nothing happened, and the task manager still shows that my service uses 150 MB of memory. The function of reading and processing a text file is very simple, and I am sure that there are no hidden links to a string array containing text. Can someone shed some light on why this is happening? Is it possible that a stream generated by another generated stream cannot clear after itself?

thanks

+4
source share
2 answers

If you use the Windows task manager to try to work out the used memory, you are likely to fool you. The memory used by the CLR usually does not return to the operating system, as far as I know ... so you can still potentially see a high working set, although most of this memory is still available for reuse within the process.

If you let the service work for a week, do you see that memory usage is constantly growing after a week, or is it just increasing on the first day, and then on a plateau? If so, do you definitely see this as a problem? If so, you may need to complete the second task in a separate process.

+5
source

Trust the garbage collector and stop worrying. 150 megabytes is nothing. You do not even measure file size; most of them will be code.

If you are worried about where the memory goes, start by understanding how memory works in a modern operating system. You need to understand the difference between virtual and physical memory, the difference between allocated and allocated memory and all this, before you start throwing numbers, for example, β€œ150 megabytes of allocated memory”. Remember that you have 2,000 megabytes of virtual address space in a 32-bit process; I would not have thought that the 150 millionth process is great.

As John says, you should worry about the slow growth of private bytes. If this does not happen, then you do not have a memory leak. Let the garbage collector do his job and not worry about it.

If you are still worried about this, then good heavens do not use task manager. Get a memory profiler and learn how to use it. The task manager is designed to check processes by looking at them from a height of 30,000 feet. You need to use a microscope, not a telescope, to analyze how a process frees the bytes of a single file.

+11
source

Source: https://habr.com/ru/post/1236510/


All Articles