3

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 release / decrease the VM size programatically in C#

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Pavan Navali
  • 242
  • 3
  • 14
  • You mean, apart from not allocating so much in the first place? What kind of memory usage are you seeing anyway? – Dean Harding May 20 '10 at 07:01
  • Good question. Memory management/usage in garbage-collected environments (like Java and .Net) always frighten me a bit. I always fear that they end up taking more memory than C++, where you have complete control over your memory, but I'm not 100% sure about it. Are there studies that compare the memory consumption in GC and non-GC environments? – Patrick May 20 '10 at 07:05
  • 1
    @Patrick I found the link I was referring to. I'm not even gonna pretend to understand all of it but here it is: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx – Josh May 20 '10 at 07:19
  • @Codeka, Initially when my applicaition starts, the VM Size for example is 78,435K. Suppose if 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. – Pavan Navali May 20 '10 at 07:23
  • @Josh, thanks for the link. I will read later (when I have some spare time). – Patrick May 20 '10 at 07:44

2 Answers2

2

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.

Community
  • 1
  • 1
Josh
  • 68,005
  • 14
  • 144
  • 156
  • Indeed, that's the reason I never trust Task Manager, but always use Sysinternal's Process Explorer, where you can explicitly see the Private Bytes and the VM size. – Patrick May 20 '10 at 07:09
  • Very true, private bytes is a much more meaningful number. – Dirk Vollmar May 20 '10 at 07:10
  • 1
    My application freezes and sometimes throws out of memory exception. – Pavan Navali May 20 '10 at 07:14
  • Yeah then you're going to have to profile to find out where all the memory is going. If you start running out of free contiguous address space you will hit out of memory errors. Large arrays and strings are the most common culprit but you could just be leaking a lot of smaller object references. (Or optionally I suppose you could ignore the problem and move to a 64 bit OS.) – Josh May 20 '10 at 07:21
  • @Patrick: From Vista on, Task Manager will show you the private bytes instead of the VM size (as on Win XP). – Dirk Vollmar May 20 '10 at 08:47
1

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.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316