Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Since using task manager to check the memory usage of my program I came up with the following questions.....

 

1) Are there any guidelines on the amount of memory that an executable should use? and how to reduce that amount if required.

 

2) When closing screens in my application garbage collection doesn't seem to immediately clear the memory. Should it ?

 

3) Since adding Crystal Reports references to my application the memory usage seems to have gone a lot higher 37mb. Is this normal ?

Posted
Since using task manager to check the memory usage of my program I came up with the following questions.....

 

I think I've asked these questions myself before. :D

 

1) Are there any guidelines on the amount of memory that an executable should use? and how to reduce that amount if required.

 

.Net manages the memory itself. It decides how much memory your program gets.

 

To the best of my knowledge, the only way to really effect this is to use pointers in C#, which is unmanaged code. I'm not sure that it would reduce memory usage, but it should make things more streamlined and quicker.

 

2) When closing screens in my application garbage collection doesn't seem to immediately clear the memory. Should it ?

 

Nope. :p

 

In the MCAD test self study book, it's stressed many times that you cannot control garbage collection and it could happen 2 seconds after your references are terminated or 5 minutes later.

 

Garbage Collection has a very low system priority. No sense in getting in the way if it's not needed ;)

 

Compare how quickly the garbage is collected (probobly with some form of output in the destructor) when you instantiate one object vs 50,000. Also try it with various background intensive programs like defrag or encoding a DivX movie.

 

The MCAD book has you run an experiment on that. I got up to 130,000 instances of a very simple class before even one garbage collection happened.

 

To put it simply: It does it when it needs to be done and can do it without getting in the way.

 

3) Since adding Crystal Reports references to my application the memory usage seems to have gone a lot higher 37mb. Is this normal ?

 

.Net is like a bandit. It'll take all the ram you have available. But it's like a Robin Hood bandit. If that RAM is actually needed somewere else, it gives back to the RAM poor Apps.

 

I had a winform app that was like clownshoes. It didn't really do anything special... maybe like a pathetic version of Calc.exe (like 1/10th the functions). It was taking up 50mb.

 

If you minimize your form, then unminimize you'll see that the memory drops drastically, but as your code runs it starts climbing back up.

 

There is a huge long complicated explaination, but here is the short deal.

 

You compile vb6, c++, Delphi, etc and it's compiled. it's in machine language and it just runs. Now it basically runs all at once except for the case of dynamic objects which can keep increasing.

 

.Net is precompiled, but isn't compiled until it's run. To be more specific only the code needed is compiled as it's run. You can have a file that would take up 100mb of memory, but it compiles what you're going to use first (probobly splash and mainform) and holds off on the rest of the application.

 

Now the trick is that once it's compiled, it's residing in memory. Memory is way faster than recompiling or reading from the disk. So until something else needs the memory, it keeps ALL of the code it's just compiled in memory. Just in case it needs it again. This is why you start up your app and it's a little slow. You click a button and open up another form and it's slow. Now do anything a second time - unless .Net had to give up that memory, the code is already compiled in machine language in memory.

 

The moral is that it's ok for .Net to take up as much memory as it likes so long as it doesn't interfere with anything elses memory requirements.

  • Administrators
Posted

Just to add Sysinternals has a nice process monitor that can show number and types of garbage collections that have happened (along with a whole load of other statistics).

If you really want to know how GC works then I would definately recomend reading Applied Microsoft .NET Framework Programming by Jeffery Richter, as well as being an excellent book in it's own right the chapter on memory management in .Net is the most comprehensive one I've seen so far.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
What is this MCAD book and where can one get a hold of it?

 

I'm reading Developing Windows Applications with Visual Basic.Net and Visual C#.Net.

 

I got it here at Amazon.

 

It's not terribly in depth, but it covers a lot of basics and fundamentals. It's a 4 book set covering win apps, web apps, Application Design (MCSD) and XML Web services. I've only started with win apps, but Web Apps is like twice the size nearly :D

 

I think it does a good job of covering a lot of fundamentals that are sometimes overlooked (like deconstruction and garbage collection) in other books.

 

In fact, if I had my way, I'd have this book as required reading to finish before choosing between VB.Net and C#.

 

The book shows the way to do everything in either language and shows what is/isn't possible in each language. I'm better with VB.Net, but I'm learning C# so I'm learning a lot. So far I've come across like 5 instances where one language can do something and other can't and it's nice to know those differences.

Posted

Cool. I'll have to pick that one up.

Please tell me one thing that VB.Net can do that C# can't?

I need to stifle a co-worker who keeps grumbling at my vb code.

Just one. I'll read the book for the rest. :p

Wanna-Be C# Superstar
Posted
Cool. I'll have to pick that one up.

Please tell me one thing that VB.Net can do that C# can't?

I need to stifle a co-worker who keeps grumbling at my vb code.

Just one. I'll read the book for the rest. :p

 

Aside from the high quality Intellisense that actually works most of the time (C# Intellisense is MUCH nicer on 2005) and debugging actually giving you all of your errors at once without having to recompile after every error fixed and more popping up each time... hmm...

 

vb allows you to create static variables within a method that persist in memory and retain their values through multiple executions of a method. In C# ALL variables go out of scope once the method is over. I think this one is pretty trivial, but has some uses.

 

VB.Net has a major one, which is optional Perameters. They let you create perameters with default values which a user can choose to use or not. you can do the same in C# with overloading methods, but thats a lot more code.

 

True, I think C# has more developed syntax but at the cost of IDE development features. Both languages have their Pro's and Cons. Most differerences arn't too overpowering and have workarounds.

Posted

That's what I do.

 

My "no-parameter function" call my "with-parameter function" with some default value... so it's not that much of code... unless you want to rewrite the WHOLE function... well... we call this monkey code at my job :p

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...