0

What I'd like to do is wrap some suspect functions that may be leaking in a using statement to trigger garbage collection, has anyone used or seen something like this? Does this work? Whats your thoughts on this?

using (String wrapper = new String("maybe this will trigger gc")){
  //do stuff here
  //or maybe:
  // function1();
  // function2();
  //
  //and then see if its leaking?

  // eg:
       _mygeometry = new Geometry();
       _mygeometry = null; // will GC free this ?
}

Do you think this will work? Have you used this before? Is there something I can do that isnt a String? Im using WPF, I tried using ( var garbage = 1 ){} and doesnt work, I suppose String might though.

jeffbeat
  • 69
  • 1
  • 5
  • 3
    what's your question? What are you trying to achieve? The `using` statement does not trigger a garbage collection. – BrokenGlass Aug 13 '12 at 03:25
  • 1
    If you want to intentionally trigger a GC, use `GC.Collect()` (usually you want to do it twice). That said, don't leave it in production code. See http://stackoverflow.com/questions/3829928/under-what-circumstances-we-need-to-call-gc-collect-twice – Chris Shain Aug 13 '12 at 03:26
  • 1
    Explicitly invoking a garbage collection (not that this does anything like that) is unlikely to fix any memory leak you are seeing in .net. You're probably going to have to find the actual leak. – recursive Aug 13 '12 at 03:53
  • thanks, I dont think its a leak, the objects just arent being freed or collected, since theres no free() or delete(). The form just seems to sit there with like 128 controls (ex-canvas children) even though they are removed from all canvas elements... they are all dynamically added at runtime... not sure what to do! – jeffbeat Aug 13 '12 at 05:42
  • C# does not use free() or delete() - I suggest reading up on what a garbage collector does: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) – Simon Ejsing Aug 13 '12 at 11:11

2 Answers2

7

using statement only works for those classes which implements IDisposable. It just makes sure that the object you defined inside the using() will call its Dispose method after the execution of the block or even when some exception occurs. It is just similar to using try with finally block.

If you are suspecting memory leakage in your application then its better if you use some of the available memory profilers to detect the problem.

Your current code shouldn't compile as String class doesn't implement IDisposable

EDIT:

Since the edited question

_mygeometry = null; // will GC free this ?

You should see this SO Link: C#: should object variables be assigned to null? and answer from VinayC

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

Only classes that implement IDisposable can be used within using(...){...} statements.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

Source: MSDN

Unfortunately, string does not implement this interface. Are you suspecting that a string is causing memory leak at your app? What are you processing? Could you post some code so we'd be able to inspect it?

Andre Calil
  • 7,652
  • 34
  • 41
  • i know, i wanted to do this: using(var k = 1){ geometry1 = new geometry(); // do stuff that results in a 200 meg geometry geometry1 = null; } hoping using would free the memory after it was done, dont think I can, WPF doesnt use IDisposable, and geometry isnt disposable, etc... Ideas? – jeffbeat Aug 13 '12 at 05:46
  • @jeffbeat It will, anytime. `GC` is not "real time", unfortunately. If you're really working with 200mb object, then you should consider working with pointers, but that's kind awkward these days. IMO, the best solution would create a new class that inherits from Geometry and implements IDisposable. – Andre Calil Aug 13 '12 at 11:26