开发者

How to release the VM / private bytes of a Desktop application in c#

I have desktop application developed in C#. The VM Size used by application is very high. I want to add watermark to a pdf file, which has more that 10,000 pages, 10776 pages to be exact, the VM size inscreases and some times the application freezes or it throws out of memory exception.

Is there a solution to rele开发者_JS百科ase / decrease the VM size programatically in C#


Environment.FailFast :)

In all seriousness though, a large VM size is not necessarily an indication of a memory problem. I always get confused when it comes to the various memory metrics but I believe that VM size is a measurement of the amount of used address space, not necessarily used physical memory.

Here's another post on the topic: What does "VM Size" mean in the Windows Task Manager?

If you suspect that you have a problem with memory usage in your application, you probably need to consider using a memory profiler to find the root cause (pun intended.) It's a little tricky to get used to at first but it's a valuable skill. You'd be surprised what kind of performance issues surface when you're profiling.


This depends strongly on your source code. With the information given all I can say is that it would be best to get a memory profiler and check if there is space for optimizations.

Just to demonstrate you how memory usage might be optimized I would like to show you the following example. Instead of using string concatenation like this

string x = "";
for (int i=0; i < 100000; i++)
{
    x += "!";
}

using a StringBuilder is far more memory- (and time-)efficient as it doesn't allocate a new string for each concatenation:

StringBuilder builder = new StringBuilder();
for (int i=0; i < 100000; i++)
{
    builder.Append("!");
}
string x = builder.ToString();

The concatenation in the first sample creates a new string object on each iteration that occupies additional memory that will only be cleaned up when the garbage collector is running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜