MDI Child not destroyed

raffy

New member
Joined
Oct 13, 2004
Messages
3
Programming Experience
1-3
HI,

tried to browsed for any forum that may answer my dilema but to no avail, so i thought of posting my inquiry here. pls do help.

i have simple MDI_Form1 with just a 2 Menu item that instantiate a child_Form2 that have NO CONTROLS or script, what happens is that once i closed the child, the memory used was not released, and once i instantiate the SAME child from the same MDI PARENT, the memory used by the project itself just continues to pile up, how come??? is this OK???



MDI PARENT CODE is


Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim xfrm As Form2
xfrm = New Form2
xfrm.MdiParent = Me
xfrm.Show()
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Me.Close()
End Sub


THE CHIlD FORM CODE is


Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Me.Dispose()
End Sub


--------------------------------

i Tested it like this..

when i run the code its around 13,828kb , when i load a NEW instantiated form , the memory is 14,000 kb.

fine, it works BUT when i CLOSE the child, the total memory that is around 14,000kb previously , is suppose to DECREASE or atleast below 14,000kb, but instead it spiked up to around 14,080kb and contineously every time i instantiate the Child_form2,

i cant IMAGINE how huge memory compsumption it will use if ever i have a bigger form with lots of controls and instantiated classes in it.

PLS do help, i have this problem for almost a month now.

immidiate reply will be much appriciated, thanks
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
that's because when you close a window (mdi children and others) it's not destroyed (memory released) untill the .net garbage collector is ran which runs on its own from time to time, so jes wait a little while and the garbage collection will happen on it's own
 

raffy

New member
Joined
Oct 13, 2004
Messages
3
Programming Experience
1-3
hi, i tried it out, and waited for almost 5 mins... and still the memory used was not released, how long should i wait, is there a way i can FORCE it to release?? am using vb.net 2003 with 1.1 framework.

thanks again in advance.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
yea, there is a way (i forget how) but microsoft strongly advises against it...i'll find ya an article or something tomarrow
 

raffy

New member
Joined
Oct 13, 2004
Messages
3
Programming Experience
1-3
Hi,

I have another inquiry.

I have this child form where i have a textbox1

i Need to access this textbox from an instantiated class,

let say

Public Class Class1
Public Function TestObj(ByVal formName As Object)

formname.TextBox1.Text = "test"

End Function

End
Class


then a code from a click button from my form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim frmClass As New Class1

frmClass.TestObj(
Me)

End Sub


when i compile this , theres no error, but when i am at the button click event, the error appears : pulic member textbox1 on type form2 not found. I know that i must pass the exact form name like this inorder for it to execute properly.
Public Function TestObj(ByVal formName As form2)

formName .TextBox1.Text = "test"

End Function

but this is not a good solution because I have to make this TestObj a generic script wherein i just need to pass a child form as a parameter inorder for it to be used by other forms, other wise i will recreate the same code over and over again with a different formname as a parameter, pls. advise.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
straight from the MSDN in VS.net 2003:

Automatic Memory Management:

Automatic memory management is one of the services that the common language runtime provides during Managed Execution. The common language runtime's garbage collector manages the allocation and release of memory for an application. For developers, this means that you do not have to write code to perform memory management tasks when you develop managed applications. Automatic memory management can eliminate common problems, such as forgetting to free an object and causing a memory leak, or attempting to access memory for an object that has already been freed. This section describes how the garbage collector allocates and releases memory.

Allocating Memory
When you initialize a new process, the runtime reserves a contiguous region of address space for the process. This reserved address space is called the managed heap. The managed heap maintains a pointer to the address where the next object in the heap will be allocated. Initially, this pointer is set to the managed heap's base address. All reference types are allocated on the managed heap. When an application creates the first reference type, memory is allocated for the type at the base address of the managed heap. When the application creates the next object, the garbage collector allocates memory for it in the address space immediately following the first object. As long as address space is available, the garbage collector continues to allocate space for new objects in this manner.

Allocating memory from the managed heap is faster than unmanaged memory allocation. Because the runtime allocates memory for an object by adding a value to a pointer, it is almost as fast as allocating memory from the stack. In addition, because new objects that are allocated consecutively are stored contiguously in the managed heap, an application can access the objects very quickly.

Releasing Memory
The garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots. Every application has a set of roots. Each root either refers to an object on the managed heap or is set to null. An application's roots include global and static object pointers, local variables and reference object parameters on a thread's stack, and CPU registers. The garbage collector has access to the list of active roots that the just-in-time (JIT) compiler and the runtime maintain. Using this list, it examines an application's roots, and in the process creates a graph that contains all the objects that are reachable from the roots.

Objects that are not in the graph are unreachable from the application's roots. The garbage collector considers unreachable objects garbage and will release the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects. As it discovers each unreachable object, it uses a memory-copying function to compact the reachable objects in memory, freeing up the blocks of address spaces allocated to unreachable objects. Once the memory for the reachable objects has been compacted, the garbage collector makes the necessary pointer corrections so that the application's roots point to the objects in their new locations. It also positions the managed heap's pointer after the last reachable object. Note that memory is compacted only if a collection discovers a significant number of unreachable objects. If all the objects in the managed heap survive a collection, then there is no need for memory compaction.

To improve performance, the runtime allocates memory for large objects in a separate heap. The garbage collector automatically releases the memory for large objects. However, to avoid moving large objects in memory, this memory is not compacted.

Generations and Performance
To optimize the performance of the garbage collector, the managed heap is divided into three generations: 0, 1, and 2. The runtime's garbage collection algorithm is based on several generalizations that the computer software industry has discovered to be true by experimenting with garbage collection schemes. First, it is faster to compact the memory for a portion of the managed heap than for the entire managed heap. Secondly, newer objects will have shorter lifetimes and older objects will have longer lifetimes. Lastly, newer objects tend to be related to each other and accessed by the application around the same time.

The runtime's garbage collector stores new objects in generation 0. Objects created early in the application's lifetime that survive collections are promoted and stored in generations 1 and 2. The process of object promotion is described later in this topic. Because it is faster to compact a portion of the managed heap than the entire heap, this scheme allows the garbage collector to release the memory in a specific generation rather than release the memory for the entire managed heap each time it performs a collection.

In reality, the garbage collector performs a collection when generation 0 is full. If an application attempts to create a new object when generation 0 is full, the garbage collector discovers that there is no address space remaining in generation 0 to allocate for the object. The garbage collector performs a collection in an attempt to free address space in generation 0 for the object. The garbage collector starts by examining the objects in generation 0 rather than all objects in the managed heap. This is the most efficient approach, because new objects tend to have short lifetimes, and it is expected that many of the objects in generation 0 will no longer be in use by the application when a collection is performed. In addition, a collection of generation 0 alone often reclaims enough memory to allow the application to continue creating new objects.

After the garbage collector performs a collection of generation 0, it compacts the memory for the reachable objects as explained in Releasing Memory earlier in this topic. The garbage collector then promotes these objects and considers this portion of the managed heap generation 1. Because objects that survive collections tend to have longer lifetimes, it makes sense to promote them to a higher generation. As a result, the garbage collector does not have to reexamine the objects in generations 1 and 2 each time it performs a collection of generation 0.

After the garbage collector performs its first collection of generation 0 and promotes the reachable objects to generation 1, it considers the remainder of the managed heap generation 0. It continues to allocate memory for new objects in generation 0 until generation 0 is full and it is necessary to perform another collection. At this point, the garbage collector's optimizing engine determines whether it is necessary to examine the objects in older generations. For example, if a collection of generation 0 does not reclaim enough memory for the application to successfully complete its attempt to create a new object, the garbage collector can perform a collection of generation 1, then generation 0. If this does not reclaim enough memory, the garbage collector can perform a collection of generations 2, 1, and 0. After each collection, the garbage collector compacts the reachable objects in generation 0 and promotes them to generation 1. Objects in generation 1 that survive collections are promoted to generation 2. Because the garbage collector supports only three generations, objects in generation 2 that survive a collection remain in generation 2 until they are determined to be unreachable in a future collection.

Releasing Memory for Unmanaged Resources
For the majority of the objects that your application creates, you can rely on the garbage collector to automatically perform the necessary memory management tasks. However, unmanaged resources require explicit cleanup. The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file handle, window handle, or network connection. Although the garbage collector is able to track the lifetime of a managed object that encapsulates an unmanaged resource, it does not have specific knowledge about how to clean up the resource. When you create an object that encapsulates an unmanaged resource, it is recommended that you provide the necessary code to clean up the unmanaged resource in a public Dispose method. By providing a Dispose method, you enable users of your object to explicitly free its memory when they are finished with the object. When you use an object that encapsulates an unmanaged resource, you should be aware of Dispose and call it as necessary. For more information about cleaning up unmanaged resources and an example of a design pattern for implementing Dispose, see Programming for Garbage Collection.

-------
Programming for Garbage Collection:

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you use the new operator to create an object, the runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

This section describes how the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. In addition, it describes the recommended design pattern to use to properly clean up any unmanaged resources that your application creates.

In This Section
Developer Backgrounds in Memory Management
Describes the adjustments developers, who have traditionally used Visual Basic, C++, and COM, should make when moving to managed code.
Finalize Methods and Destructors
Describes how Finalize methods and destructors allow an object to perform necessary cleanup operations before the garbage collector automatically reclaims the object's memory.
Cleaning Up Unmanaged Resources
Describes the recommended design pattern for cleaning up unmanaged resources. This section provides code examples for the following tasks:
Implementing a Dispose Method
Overriding the Finalize Method
Using C# and Managed Extensions for C++ Destructor Syntax
Forcing a Garbage Collection
Describes how and when to force the garbage collector to perform a collection.
Related Sections
GC Class
Provides methods for interacting with the system garbage collector.
Object.Finalize Method
Allows an object to attempt to free resources and perform other cleanup operations before the garbage collector reclaims the object.
IDisposable Interface
Provides the functionality for a resource class.
Garbage Collection Technology Sample
Introduces the functionality of the .NET Framework garbage collector.

-------
Forcing a Garbage Collection:

The garbage collection GC class provides the GC.Collect method, which you can use to give your application some direct control over the garbage collector. In general, you should avoid calling any of the collect methods and allow the garbage collector to run independently. In most cases, the garbage collector is better at determining the best time to perform a collection. In certain rare situations, however, forcing a collection might improve your application's performance. It might be appropriate to use the GC.Collect method in a situation where there is a significant reduction in the amount of memory being used at a defined point in your application's code. For example, an application might use a document that references a significant number of unmanaged resources. When your application closes the document, you know definitively that the resources the document has been using are no longer needed. For performance reasons, it makes sense to release them all at once. For more information, see the GC.Collect Method.

Before the garbage collector performs a collection, it suspends all currently executing threads. This can become a performance issue if you call GC.Collect more often than is necessary. You should also be careful not to place code that calls GC.Collect at a point in your program where users could call it frequently. This would defeat the optimizing engine in the garbage collector, which determines the best time to run a garbage collection.
 
Top Bottom