There is no method Close
, Finalize
is protected, sending WM_CLOSE
also gives nothing.
2 answers
You need to remove it from the form:
Controls.Remove(userControl);
And in order to completely kill, you need to clear all references to it and call the garbudge collector, but usually this is unnecessary.
GC.Collect();
- Also, if the control implements the
IDisposable
interface, you need to call theDispose
method after removing the control from the collection. Note: explicitly calling garbage collection is not very recommended. First of all, it’s best to see, and secondly, the call toGC.Collect()
does not guarantee the release of memory from under those objects that are not referenced. - andreycha - @andreycha; The
Dispose
method explicitly frees unmanaged resources that are also managed to be marked for removal by the garbage collector. AlsoGC.Collect()
[guarantees] [1] freeing memory of those objects that do not have references in the managed code. Also, if you read [here] [2], then Dispose does not guarantee the release of the managed memory occupied by the object - it prepares it for reuse. [1]: msdn.microsoft.com/ru-ru/library/xe0c2357 [2]: msdn.microsoft.com/ru-ru/library/… - Chad - one1) It depends on how it is implemented. It may or may not. 2) In general, does not guarantee. Since if the object has the
Finalize()
method defined, then after the first collection the object will be moved to the next generation and put in the finalization queue. And only after the finalizer has completed and the assembly reaches this generation, will the memory be released. It is because of the filling of such subtleties that they are not recommended to command the collector explicitly. 3) Yes, I know. But if you want to part with the object, then this method is highly desirable to cause (and the question was about that). - andreycha
|
- Do not be misleading, as well as read the documentation. Calling
Dispose()
does not destroy the object and does not free the memory it occupies. TheDispose()
method is designed to release the resources that this class uses. - andreycha - oneIs it about freeing the memory? - AlexeyM
|
(control.Visible = false)
? Remove it from the form? Free up their resources? - Modus