How Much memory do my objects use?

  • Thread starter Thread starter gregory_may
  • Start date Start date
G

gregory_may

Does VB/Visual studio.Net have some way to let me know how much memory my
objects are taking up? I cant seem to figure out how to get the
Locals/Watch/or Memory windows to show this information (Maybe I am doing it
wrong?).

It looks like I have some kind of memory leak in my Visual Basic program. I
am grabing about 250K of memory every 10 seconds (Acording to Task Manager).
I have all kinds of stuff
happening (Threads spinning in the backround & all kinds of timers going
off) and I cant tell where memory is getting sucked up.

Some kind of function I can execute from the code would be fine
too like:

HowMuchMemoryDoesThisSillyObjectUse(MyObject)


I put a GC.Collect statement in one of my timers, it is keeps memory usage
more "stable", but it feels like such a HACK!

Any help would be great!

g.
 
Hello,

Thanks for your post. Generally speaking, VB and VB .NET programs rarely
leak memory, because VB code does not have pointers and do not dynamically
allocate memory directly. In addition, owing to the garbage collection
package that is implemented in the Microsoft .NET Framework, it is not
possible to have a memory leak in managed code.

This suggests two questions: How then can a memory leak occur? Why does it
appear that you have a memory leak?

A memory leak can occur in a .NET Framework application when you use
unmanaged code as part of the application. This unmanaged code can leak
memory, and the .NET Framework runtime cannot address that problem.

Additionally, a project may only appear to have a memory leak. This
condition can occur if many large objects (such as DataTable objects) are
declared and then added to a collection (such as a DataSet). The resources
that these objects own may never be released, and the resources are left
alive for the whole run of the program. This appears to be a leak, but
actually it is just a symptom of the way that memory is being allocated in
the program.

I believe the following MSDN articles are helpful:

INFO: Identify Memory Leaks in the Common Language Runtime
http://support.microsoft.com/?id=318263

INFO: Roadmap for Debugging Hangs, Memory Leaks, Deadlocks, and Race
Conditions in Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317297

You can also go to "Control Panel" -> "Administrative Tools" ->
"Performance" to check the memory performance counters in the Performance
Monitor:

Memory Performance Counters
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/gngrfmemoryperformancecounters.asp

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
In addition, owing to the garbage collection
package that is implemented in the Microsoft .NET Framework, it is not
possible to have a memory leak in managed code.

That's entirely false. A number of instances exist wherein the Garbage
Collector in .NET fails to free allocated resources creating memory leaks.
In addition, irresponsible use of objects as well as improper management of
objects which wrap unmanaged entities also promotes memory leaks. Tools like
the Allocation Profiler were written, in part, to solve memory leak issues
with .NET
 
I know GC.Collect is a bad idea. It is "stabilizing" my memory leak, but
still not fixing things. I am really amazed there isnt a view to hint at
where my leak might be (Ok its really my fault for writing "bad" code, but
memory is still leaking away). I am currently suspecting this line of code
wich can get executed many-many times/second:
AddHandler LED_Blink_Timer.Elapsed, New
System.Timers.ElapsedEventHandler(AddressOf Me.LED_Blink_Timer_Elapsed)

But since I cant figure out how DotNet is allocating memory, I am shooting
in the dark.

g.
 
It appears I have a memory Leak because TaskManager shows my program
allocating about 250K of RAM every 10 - 15 seconds.

You can call it bad code, a memory leak or whatever, (or God forbid some bug
in .Net). I just need to fix it.

I was hoping that .Net had a great way for me to figure out where I messed
up, some kind of memory profiling tool or something??? I will look over
your post to see what I can come up with.

Thanks!
g.
 
Back
Top