Variable free in VB.NET

  • Thread starter Thread starter Lurc
  • Start date Start date
L

Lurc

Dear all,

I am a C programmer, and new to VB.NET.

In C, any user-defined variables allocated by new keyword, they need to be
de-allocated by free within scope. But in VB.NET, it seems not need to do
de-allocation manually, does system do it for me automatically?

Thanks.
 
Lurc,

Net uses in its managed programming languages, what is named managed code.

One of the purposes is to free the programmer from all those things that
he/she knew the OS could do it self however, it was never done. Although in
my opinion there can be done a lot more.

I hope that this gives an idea.

Cor
 
: Dear all,
:
: I am a C programmer, and new to VB.NET.
:
: In C, any user-defined variables allocated by new keyword, they
: need to be de-allocated by free within scope. But in VB.NET, it
: seems not need to do de-allocation manually, does system do it
: for me automatically?
:
: Thanks.
:
: --
: Best regards,
: RL


Yes. .Net, like Java before it (and Visual Basic before *it*),
automatically handles memory allocation and de-allocation for managed
code. This is the "garbage collection" feature of .net and when a
variable falls out of scope, it is flaged for release by the framework.
Note that this does not mean the memory is necessarily recovered as soon
as when the variable is not longer accessible, only that it is free to
be released when the frame work determines it is appropriate to do so.


Fwiw, while I'm a big proponent of VB (and personally prefer it to other
..net languages), you may feel more at home using C# in transistioning
from C as the syntax is much closer. As I understand it, there is also a
..net (i.e.: managed) version of C++ as well. In this case, you can
intermingle unmanaged code within the managed should there be a need to
do so.


Ralf
 
Speaking of which, Cor, how does the garbage collection system work?
Meaning, I know in some cases it's best to call dispose methods yourself
instead of letting the framework handle this, but how do you know when? At
what point do objects actually get disposed (ie, when they become out of
scope, when the whole program closes, etc...)?
 
OpticTygre said:
Speaking of which, Cor, how does the garbage collection system work?
Meaning, I know in some cases it's best to call dispose methods yourself
instead of letting the framework handle this, but how do you know when?
At what point do objects actually get disposed (ie, when they become out
of scope, when the whole program closes, etc...)?

I suggest to read the chapter about garbage collection in the documentation:

..NET Framework Developer's Guide -- Programming for Garbage Collection
<URL:http://msdn.microsoft.com/library/e...programmingessentialsforgarbagecollection.asp>
 
Optic Tygre,

Questions about the dispose becomes mostly a kind of religious debates in
these Net newsgroup.

First the dispose and the GC are only slightly related to each other.
Dispose is not what some people think the Finalize method from Net

The command is only telling to the GC that an object is ready go get
investigated, next time that the GC is running, if it can be finalized. If
that is not possible, than nothing happens. And because of the fact that
mostly Idisposable is implemented and the dispose is explicitly done, in my
opinion in most cases where it can be done (not all) a little bit senseless.
The GC does it himself without the dispose as well, however that can be a
little bit late. I have add a link about that at the end.

Some people who don't understand the dispose (what I do also not completly)
make it themselves easy. They write: "Just call it when you see it, they
did not make it for nothing". However it is in almost every class because
the dispose is inheritted from component model, from which almost all
system.controls and system.data classes inherit.

I don't do in every program as well not something with "Equals", just
because it exist in every class.

See this message from Jay sent some days after a very heat discussion about
this subject.

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/3e55c043b1e6ae59?hl=en

Reading a little bit through the lines where Jay is not sure himself,
describes it the problem good. I have until now not seen something betters,
and almost forever ends an artickle with that text "When you are not sure,
just use dispose" What makes that the writter is for me not sure from
himself.

About the GC is a very nice description in this and the next topic of that
page.
http://msdn.microsoft.com/architect...ml/scalenetchapt05.asp#scalenetchapt05_topic9

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top