473,668 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forms and Memory management question

Suppose I do this: let's say that in my vb.net app's main/MDI form, I
declare a form-level variable of type Form1, where Form1 is another
form (more technically, I know, another class which itself is inherited
from system.windows. forms.form) defined in my application.

dim mfrmForm1 as Form1

Okay, now let's also say I put two menu items on my MDI form, and code
them something like this

(under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

....okay so far; now let's say at run-time, the user clicks this first
menu item, thereby generating and showing an instance of Form1. (Let's
also assume there's no code behind Form1 except for that generated by
the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand
corner. Finally, let's say the user then selects the second menu item
on the MDI parent form, under which, let's say!, we have this code:
(same as the other menu item)

(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

....thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method
explicitly called in code, nor was it set to Nothing anywhere. So
(here's my main question) does it still leave some kind of memory
footprint? Maybe the first Form1 object still exists in some sense
even though it was closed....it is still in scope, because it's
declared as a module-level variable inside the MDI form, so I imagine
the garbage collector not recognize it? (If the instance of Form1 was
declared locally within a procedure, then my guess would be that things
would be different: it would go out of scope, and the GC would get rid
of it for me before too long, whether or not I disposed of it or set it
to Nothing in code.) Could something like this cause a performance
deterioration or memory leak eventually? Could/should/would the
situation be improved by adding this event in the MDI form's code
window:

Private Sub mfrmForm1_Close d(ByVal sender As Object, ByVal e As
System.EventArg s) Handles mfrmForm1.Close d
mfrmForm1.Dispo se()
mfrmForm1 = Nothing
End Sub

would this make a difference; is it an improvement,
memory-management-wise?

Thanks very much for reading,

richforman

Nov 21 '05 #1
7 1593
> (under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand
When a form is shown by calling the Show method, it is automatically
disposed when it is closed.
(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

...thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method
The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.
(here's my main question) does it still leave some kind of memory
footprint? Maybe the first Form1 object still exists in some sense
even though it was closed....it is still in scope, because it's


It does exist, but it has been disposed and is eligible for GC now.

Nov 21 '05 #2
Chris Dunaway wrote:
(under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand

When a form is shown by calling the Show method, it is automatically
disposed when it is closed.

(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

...thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method

The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.

(here's my main question) does it still leave some kind of memory
footprint? Maybe the first Form1 object still exists in some sense
even though it was closed....it is still in scope, because it's

It does exist, but it has been disposed and is eligible for GC now.


"When a form is shown by calling the Show method, it is automatically
disposed when it is closed."

Is that true? It thought it was only disposed of when their were no
more pointers to the form. If what you were saying were true then I
couldn't do something like:

class XYZ
dim f as form1

sub buttonclick(..) handles button.click
f.show
end sub

end class

click button once, close the form and then click the button again.

Chris
Nov 21 '05 #3
Thanks so much to both you guys for your responses, let me keep the
discussion going with another question or two:

Chris Dunaway wrote:
(under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand


When a form is shown by calling the Show method, it is automatically
disposed when it is closed.


Now, I didn't know this, and to tell you the truth, from my testing,
I'm not really convinced that this is correct. It might be, but I
think it's not the essential point for what I'm trying to find out, so
let me ask about the next point for my own understanding:
(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

...thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method


The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.


This is what I really wanted to know, so let me reiterate this and
verify that we're all in agreement on this. I suspected this but
wasn't sure....and this is what you say here, so I just want to make
sure it's true (so I can report back to my boss about this
issue!)....if the object is no longer pointed to by any variable, then
it is will be GC'd, even though if it still "exists"?

If this is the case, then I have to keep looking for the cause of the
performance reduction some users of my app (but not all) are
experiencing over time.

richforman

Nov 21 '05 #4
rf******@optonl ine.net wrote:
Thanks so much to both you guys for your responses, let me keep the
discussion going with another question or two:

Chris Dunaway wrote:
(under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand


When a form is shown by calling the Show method, it is automatically
disposed when it is closed.

Now, I didn't know this, and to tell you the truth, from my testing,
I'm not really convinced that this is correct. It might be, but I
think it's not the essential point for what I'm trying to find out, so
let me ask about the next point for my own understanding:

(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

...thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method


The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.

This is what I really wanted to know, so let me reiterate this and
verify that we're all in agreement on this. I suspected this but
wasn't sure....and this is what you say here, so I just want to make
sure it's true (so I can report back to my boss about this
issue!)....if the object is no longer pointed to by any variable, then
it is will be GC'd, even though if it still "exists"?

If this is the case, then I have to keep looking for the cause of the
performance reduction some users of my app (but not all) are
experiencing over time.

richforman


Yes, if nothing has a reference to it, then it will be GC'd whenever the
GC gets around to it... It won't happen right away. There are memory
monitors around for .Net that should help you. You can't rely on
taskmanager for an accurate monitor.

Chris
Nov 21 '05 #5
ng
There's a really good way to avoid the possibility of this thing lingering:
Declare in the menu click event your form variable as new form1. While
the form will be usable, the variable is already out of scope, because
we have already exited the menu click event. When the form is closed,
no matter how, there is nothing referencing it anymore. The GC won't be
relied upon so heavily.

Tom
Chris wrote:
rf******@optonl ine.net wrote:
Thanks so much to both you guys for your responses, let me keep the
discussion going with another question or two:

Chris Dunaway wrote:
(under MenuItem1's click event)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.show

the designer.) Let's further say that the user then closes this first
instance of Form1 using the control-box "X" at its upper right hand
When a form is shown by calling the Show method, it is automatically
disposed when it is closed.

Now, I didn't know this, and to tell you the truth, from my testing,
I'm not really convinced that this is correct. It might be, but I
think it's not the essential point for what I'm trying to find out, so
let me ask about the next point for my own understanding:

(under MenuItem2's click event:)

mfrmForm1 = new form1
mfrmForm1.MDIPa rent = me
mfrmForm1.Show

...thereby creating a second instance of Form1. Now, the original,
first instance of Form1 never had its .Close or .Dispose method
The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.

This is what I really wanted to know, so let me reiterate this and
verify that we're all in agreement on this. I suspected this but
wasn't sure....and this is what you say here, so I just want to make
sure it's true (so I can report back to my boss about this
issue!)....if the object is no longer pointed to by any variable, then
it is will be GC'd, even though if it still "exists"?

If this is the case, then I have to keep looking for the cause of the
performance reduction some users of my app (but not all) are
experiencing over time.

richforman


Yes, if nothing has a reference to it, then it will be GC'd whenever
the GC gets around to it... It won't happen right away. There are
memory monitors around for .Net that should help you. You can't rely
on taskmanager for an accurate monitor.

Chris


Nov 21 '05 #6
Your code would not run because f is never instantiated. But I did
test your example and gtt an exception when calling show after the form
was closed:

Cannot access a disposed object named "Form2".

This does not happen when the form is shown using ShowDialog.
From the docs:


"Note When the Close method is called on a Form displayed as a
modeless window, you cannot call the Show method to make the form
visible, because the form's resources have already been released. "

Nov 21 '05 #7

ng wrote:
There's a really good way to avoid the possibility of this thing lingering:
Declare in the menu click event your form variable as new form1. While
the form will be usable, the variable is already out of scope, because
we have already exited the menu click event. When the form is closed,
no matter how, there is nothing referencing it anymore. The GC won't be
relied upon so heavily.

Tom

Thanks, that won't work for me in this case because I made it a
form-level variable for actual reasons where I might need to look for
or do other things with the form, if it is currently loaded, from other
functions outside the menu-click where it is created. But thanks again
for the response.
richforman

Chris wrote:
rf******@optonl ine.net wrote:
Thanks so much to both you guys for your responses, let me keep the
discussion going with another question or two:

Chris Dunaway wrote:

> (under MenuItem1's click event)
>
> mfrmForm1 = new form1
> mfrmForm1.MDIPa rent = me
> mfrmForm1.show
>
> the designer.) Let's further say that the user then closes this first
> instance of Form1 using the control-box "X" at its upper right hand
When a form is shown by calling the Show method, it is automatically
disposed when it is closed.

Now, I didn't know this, and to tell you the truth, from my testing,
I'm not really convinced that this is correct. It might be, but I
think it's not the essential point for what I'm trying to find out, so
let me ask about the next point for my own understanding:
> (under MenuItem2's click event:)
>
> mfrmForm1 = new form1
> mfrmForm1.MDIPa rent = me
> mfrmForm1.Show
>
> ...thereby creating a second instance of Form1. Now, the original,
> first instance of Form1 never had its .Close or .Dispose method
The mfrmForm1 variable now points at the *new* form instance. Since
nothing points to the first instance anymore, it is eligible for GC.

This is what I really wanted to know, so let me reiterate this and
verify that we're all in agreement on this. I suspected this but
wasn't sure....and this is what you say here, so I just want to make
sure it's true (so I can report back to my boss about this
issue!)....if the object is no longer pointed to by any variable, then
it is will be GC'd, even though if it still "exists"?

If this is the case, then I have to keep looking for the cause of the
performance reduction some users of my app (but not all) are
experiencing over time.

richforman


Yes, if nothing has a reference to it, then it will be GC'd whenever
the GC gets around to it... It won't happen right away. There are
memory monitors around for .Net that should help you. You can't rely
on taskmanager for an accurate monitor.

Chris


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5315
by: Henro V | last post by:
Can I integrate 3 existing forms (all based on the same query) integrate in one form by using Tab's? Thanx for thinking, Henro
9
2014
by: Rafael Charnovscki | last post by:
I can comprehend the basics of that subject (pointers, memory allocation, heap, stack etc), but I am interested to have references with more details. I have some C/C++ books and lots of URLs "bookmarked", but all of them don't go too far on memory management as I would like. One of my main issues is whether function "main" and its local variables are allocated on the stack area as other functions. Does anybody recommend a good book or...
8
9326
by: Chad | last post by:
hello, i am losing memory each time i make this call to a C++ dll (I make frequent calls). 'in VB.Net Declare Function grab Lib "grabber.dll" _ (ByRef lpBuf As Byte, ByVal lnum As Integer) As Integer the C++ function is...
6
9416
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were open/closed and their current state. now i can't. before i didn't have to declare numerous named instances each time i loaded or unloaded a form.
1
2794
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots of memory. Suppose i run first C# application which has occupied all memory and
13
1852
by: Vijay | last post by:
Hi All, I am learning C++ and have one question. Using free or delete we can release the memory. After releasing memory where this released memory will go.. Does it go to back operating system? Please explain. Thanks in advance.
3
5311
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
3
1363
by: Indian.croesus | last post by:
Hi, Going through previous posts, apparently the memory management is not something that the standard C defines. I have read various posts and the word compilers and OS implementations have been used often. My confusion is what is the line, if any, that divides the two? What part of it is compiler dependent and which one is OS? Perhaps this is OT here and more specific to compiler and OS community but I could not figure out how to ask a...
5
24707
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.