0

I've made a simple .net application that takes data from a database and saves as an excel document. Problem i have noticed is that once the process has completed and the spreadsheet is generated, the application hogs about 750mb of memory. It is collating a big report, But i thought once it had saved that memory is deallocated?

Is this the case?

Thanks

MrPink
  • 1,325
  • 4
  • 18
  • 27
  • Where did you get the `750 mb of memory` from? You could be looking at the `Working Set` which would be a bad metric to use since pages remain in the Working Set of a process even if they are not in use. Make sure you're looking at private bytes – Conrad Frix Jan 06 '12 at 15:25

5 Answers5

2

In .Net memory isn't necessarily deallocated automatically.

You can try to Dispose() all large objects that implement IDisposable and/or null out any references to these instances, and then call GC.Collect() to force an immediate garbage collection. This may help if you are holding references to large objects after you are done with them.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • 1
    If you're going to vote it down, please provide a comment as to what is wrong with the answer. For my part, this answer is better suited to a comment on the original question. – RQDQ Jan 06 '12 at 15:25
  • I got 2 downvotes almost instantly. I'd really appreciate the downvoters to at least leave a comment as of where I'm headed in the wrong direction. – Dennis Traub Jan 06 '12 at 15:29
  • @DennisTraub it's better suited as a comment, since your asking a question and not providing a direct answer. – msarchet Jan 06 '12 at 15:30
  • He was making a statement about how garbage collection works and then offering a solution. How is this not an answer? – Emerica. Jan 06 '12 at 15:33
  • Your answer was more of a WAG and a question back to OP, which I'd think you'd know isn't exactly kosher around here (5k rep and all). I believe my edit captures the core of your answer without making it appear to be a comment. Also, you're right; .the CLR doesn't deallocate memory unless it has to. Memory hogging is a *good thing*. –  Jan 06 '12 at 15:36
  • @msarchet I wasn't aware of the fact that the correct choice of sentence structure matters that much. As a non-native speaker I'll be working on my English skills right away. Sorry and thanks for the feedback. – Dennis Traub Jan 06 '12 at 15:38
  • @Will thanks for the editing and clarification. And yes, even with 5k rep I wasn't aware of this type of answer not being kosher. Sorry again, English is not my first language. – Dennis Traub Jan 06 '12 at 15:41
  • @DennisTraub: Understandable. Also, you're more than welcome. –  Jan 06 '12 at 15:43
2

Memory is not deallocated instantly by the Garabage Collector.

Also are you making sure that you are disposing of all of your objects properly, and allowing the garbage collector to pick up the items. In you thoughts of after it is saved, doesn't matter if the item is still being referenced by something in your application.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • 1
    Particularly Excel resources as these can take up a lot of memory and could be the cause – Iain Ward Jan 06 '12 at 15:25
  • This is true. I have a background worker running to produce the excel document. The Garbage collector will run to deallocated any memory thats no longer being used by the application? GC.Collect() (from the post below) – MrPink Jan 06 '12 at 15:38
2

Try using a memory profiler. Here are some product links:

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

http://www.jetbrains.com/profiler/

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
1

If the data is still referenced and used by your application, then the memory will not be garbage collected. See garbage collection

Also see this previous question may help? I found it very useful, especially Igor Zevaka's answer.

Community
  • 1
  • 1
Jeb
  • 3,689
  • 5
  • 28
  • 45
0

Check for definitions. For example if you defined forms in your code it will use a lot of ram because of starting same form a lot of times.

For example; I have 3 forms in my application. Form1, Form2 and Form3.

namespace RAMAPP 
{
    public partial class RAMAPP : Form
    {
       Form1 first = new Form1();
       Form2 second = new Form2();
       Form3 third = new Form3();

    public RAMAPP()
    {
       InitializeComponent();
    }
}
}

In here, when you click start button, program reads all lines you wrote now. And before starting, RAMAPP program defining Forms as first,second and third. The problem is to define these forms before the initializing. You should delete all form definitions before the initializing. After this, restart your program and it will be run hopefully :)

Colchian
  • 1
  • 3