Memory Leak?

10/15/2010 23:16 ryuchetval#1
Ok guys...3rd thread about this....
Q1: Does the variables that are declared like int x = new int(); use memory if not set to null after it's no longer used?
Q2:is there a way to delete a variable if it's no longer used?
Q3:is it of any use to set the variables/arrays/hastables etc to null after not being used anymore?
Q4:when does GC.Collect() collects the garbage (resources)?

I am using GC.Collect in a thread running every 1 second and I've also set the variables to null after their no longer used but it seems just like more and more memory is used to idk o.O

If you can't give any answer it's good anyway :)
10/16/2010 04:48 _tao4229_#2
Addressing the title: you can't have a 'memory leak' in C# unless you're calling C functions. Using the FCL you won't get any.

ValueTypes (ints, doubles, shorts, etc) don't use heap memory, they use stack memory. Stack memory is cleared after the function exits.

Classes such as arrays/hashtables are managed and consume memory. You can't explicitly delete them from memory; the GC will handle it when it goes out of scope. C# is garbage collected, you don't need to worry about memory management.

It's not of any use really as the object will be cleaned up when it goes out of scope anyways.

The GC compacts and frees resources periodically when it needs to. If your program isn't tight for memory it won't bother running too frequently (as it does have to interrupt your program). You shouldn't call it explicitly as the algorithm it has on its own will be the most efficient 95% of the time.

If you really want to learn I'll give a better explanation of why later.
10/16/2010 07:55 ryuchetval#3
Ye I wanna learn badly cuz sometimes my source consumes 300 mb in 3-4 hours...and sometimes in 7-8 hours o.O...
10/20/2010 00:48 andyd123#4
One of the most likely problems you have is either, as tao said, relating to the size of your hashtables and dictionary entries, so forth.

Use ANTS, or something like it, and you can find out what variables are taking up the most space, and for the most part can help you defeat them. Afaik the GC will -not- dispose of any object inside of a hashtable or collection unless the collection is disposed of.

tl;dr: Collections are probably your problem.
10/20/2010 05:15 KC Royals#5
i have one
10/20/2010 08:14 ryuchetval#6
can you tell me what does ANTS mean?
10/20/2010 12:19 Fish*#7
Hybrids video tutorial explain alot about memory use of variables :)
I think u should watch it :)
10/20/2010 22:33 ryuchetval#8
well i believe collections are my problem...not sure tho...also I've noticed that if i put .clear instead of = null; to a collection after I don't need it anymore it doesn't free any memory at all...
10/20/2010 22:52 Fish*#9
Well hybrids videos are awesome, they explain alot and what could cause problems and so and he tells alot about collections wich I really like.
Its awesome.
Also he explains why u get alot random null exceptions.
Wich is awesome :D

I just <3 His videos.
10/20/2010 23:24 andyd123#10
Quote:
Originally Posted by ryuchetval View Post
well i believe collections are my problem...not sure tho...also I've noticed that if i put .clear instead of = null; to a collection after I don't need it anymore it doesn't free any memory at all...
If you clear a collection, it removes the objects from the collection, allowing the GC to dispose of them provided they are not referenced anywhere else.
Clearing the collection will not instantly free any memory. The GC will only attempt to free memory when it needs to, as Saint said. Think of it like....investing in stock? If you put $100 into stock, and you see it's worth 1000 now, that doesn't mean for sure that you will take that money out then, right? If you see it raise or fall more, you may want to quickly though. If the GC doesn't see a -need- to free memory, it wont, and it shouldn't, because it has to interrupt the program to do so(making it slower..).

Connections are probably not your problem, it's possible, but I see it as far less likely.
10/21/2010 08:15 ryuchetval#11
Quote:
Originally Posted by Fish* View Post
Well hybrids videos are awesome, they explain alot and what could cause problems and so and he tells alot about collections wich I really like.
Its awesome.
Also he explains why u get alot random null exceptions.
Wich is awesome :D

I just <3 His videos.
could you point me to this video? I couldn't search for his name/thread

Quote:
Originally Posted by andyd123 View Post
If you clear a collection, it removes the objects from the collection, allowing the GC to dispose of them provided they are not referenced anywhere else.
Clearing the collection will not instantly free any memory. The GC will only attempt to free memory when it needs to, as Saint said. Think of it like....investing in stock? If you put $100 into stock, and you see it's worth 1000 now, that doesn't mean for sure that you will take that money out then, right? If you see it raise or fall more, you may want to quickly though. If the GC doesn't see a -need- to free memory, it wont, and it shouldn't, because it has to interrupt the program to do so(making it slower..).

Connections are probably not your problem, it's possible, but I see it as far less likely.
Well...when the ram usage reaches 1/1.2 GB it still doesn't go down at all...furthermore it will crash after this amount of RAM at any time...as it happens all the time
10/21/2010 08:20 Arcо#12
Quote:
Originally Posted by ryuchetval View Post
could you point me to this video? I couldn't search for his name/thread
its stickied in the programmign section.
10/21/2010 15:31 Fish*#13
Quote:
Originally Posted by ryuchetval View Post
could you point me to this video? I couldn't search for his name/thread
infamousnoone = hybrid.
10/21/2010 22:30 ryuchetval#14
thanks for your help guys...hope i'll get it fixed
10/22/2010 19:32 ryuchetval#15
1st...sorry for double post...
2nd...what's better for collections.... clear or null? (sry if it's in the hybrid's tuts...I didn't get to watch them)