Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2008, 02:18 PM
Authorized User
 
Join Date: Jun 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Memory usage: new and dispose

And I have some other (related) question wich are on my mind for some time now:

Let's say I have a sub like this:

sub Bla
  dim TestObject as new cObjectClass
end sub


What will happen when I call this sub? It will initiate a new object, called TestObject of the type cObjectClass. But what after the sub has ended? Will the object stay in memory somewhere, because I didn't dispose it? Or will it be gone (and free-d memory) after the sub is done?




Next to this I'd really like to know if there are differences between the dispose-method, wich some objects have and settings the object to nothing to free the memory. So, wich way is best to free memory if you're not planning to use the object anymore?



And the last one which I don't understand:
I have two objects, let's say:
dim Human1 as new cHuman
dim Human2 as new cHuman

Now I edit Human1:
Human1.name = "Christopher"
Human1.age = 26

And then I want Human2 to be exactly as Human 1:
Human2 = Human1

Question: What do I have here now? Two different objects with both the same settings? Or one object with it's setting and the otherone pointing at the first one? In other words; if I now change the name of Human1, will it change the name of Human2 because it's a reference to the same object? Or will Human2 stay the same as before?

And is there a difference if I would have declared the humans as:
dim Human1 as new cHuman
dim Human2 as cHuman

??

Maybe there is a reference on the internet you could advise me to read about this subject?

In every case, many thanx, 'cause I really would like to know the details.

 
Old August 7th, 2008, 05:04 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

What will happen when I call this sub?

The object will be created, and a reference to it through TestObject will be created, and the reference count will be boosted to 1.
When TestObject goes out of existence, the reference count is dropped to 0. When garbage collection takes place, the memory will be reclaimed.
Because of this, you cannot rely on actions within the cObjectClass class to perform their finalizations just because the reference went away.

The Dispose method will run when the object is garbage collected, but you cannot know when that will be.
Setting to Nothing opens the door for that garbage collection, but nothing more (no pun intended).
Having or not a Dispose method makes no difference as to when the memory will be freed. If memory is needed, a GC will be triggered. This wway, the program is not slowed down by destroying objects every time they are no longer referenced, but only when memory is required.

When you run the two As New statements two objects are created, and a reference to each is created. They each have a reference count of 1.

Human2 = Human1 causes the obect that Human1 refers to to now have a reference count of 2, and causes the object that Human 2 used to refer to to now have a reference cout of 0—it can be destroyed upon GC. Human2.name = "Christopher," because it reads the same memory as was set and filled through Human1.

In your final example, the 2nd statement types the variable, but creates no object—and it does not change the size of your program. Variable names are used in compilation, but do not exist in the actual code. If an object is not created, no memory for it is ever needed.
But, accordingly, if you then refer to Human2 in code, you will recieve object or block not initialized (or whatever). You will need to either do something like Human2 = Human1, where Human 1 is an initialized object, or Human2 = New cHuman. The original statement, having typed the variable will cause a compilation error if you try Human2 = New cUberMan. That is a different type. That typing is not useless, you see. Just because it creates nothing, doesn't mean it doesn't do anything for you.

Does that clarify?
 
Old August 9th, 2008, 03:17 AM
Authorized User
 
Join Date: Jun 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow! Thanx a lot for using your fine explaining skills, this realy clarifies the thing!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dispose Pattern guntank C# 0 September 26th, 2006 10:08 PM
Dispose() pzmrcd .NET Framework 1.x 2 March 2nd, 2006 03:19 PM
Question : Dispose mike_abc Pro VB.NET 2002/2003 1 May 14th, 2005 02:17 PM
dispose Ibn_Aziz C# 3 February 26th, 2004 05:17 AM
Regarding Memory Usage Asish SQL Server 2000 4 November 11th, 2003 07:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.