?
Solved

memory management

Posted on 2003-03-30
9
Medium Priority
?
205 Views
Last Modified: 2010-04-15
how do i create an object or any other variable and destroy it from the memory as and when required. I need this procedure in details cause i am developing a system which uses " GOTO " function extensively. as you all know the goto function causes entered details to linger around in the memory. I want to get rid of this memory. What should i do ? Please do guide me. I could also i could also do with some books to refer to. Thank You.
0
Comment
Question by:technova0
7 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 100 total points
ID: 8234033
.NET essentially ELIMINATES the ability to 'manage' objects/memory yourself.  Rather that using DETERMINISTIC destruction (an object is removed from memory at a specific time, under the control of the program), .NET uses Garbage Collection (NON-DETERMINISTIC desctruction), so that unused objects are removed from memory when the need to the space arises.  That means that the programmer has very little control over EXACTLY when that actually occurs.

It is also VERY VERY bad programming practice to use the GOTO statement.  An not simply for the reason that you mentioned.  GOTO leades to code that is ALMOST impossible to maintain (also known as 'spaghetti code').

AW
0
 
LVL 1

Expert Comment

by:sparticus1701
ID: 8240222
I agree that goto is always a bad idea.

When the managed heap is looking for things to collect, it checks each item and looks to see what is still referencing it.  If no one has a reference to it, it can be marked for later deletion.

Someone correct me if I'm wrong, but if you use a goto, references may still exist, even though you may not come back to that code.

Rather than delete them explicitly, the best you could do is set the variable to null, eliminating the reference.  That will help the garbage collector, but I don't think it's a real solution to the problem.
0
 
LVL 2

Expert Comment

by:nir2002
ID: 8244014
Hi,

to help the garabage collector, after finish using a object assign null to his reference.

The garbage collector will free space of object only when there is no reference to this object.

I recommend you as me fellow members to avoid goto and to use an O.O method.

Best regards
Nir
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 100 total points
ID: 8248467

Arthur_Wood is mostly correct in what he said.  If left to its own devices the .NET framework destroys objects as it sees fit.  This is great for most applications (reduces the worry of memory leaks and such).  However, if you create several very large object and then you want to recover the memory right away, there is a way to do this.

System.GC.Collect();

this code explicitly calls the garbage collector.  There is, of course, a price to pay for this garbage collection.  You will take a small performance hit when this code is called, but sometimes it's worth it.  That's up for you to decide (just keep this code out of a loop and you should be fine).
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8248523
There is one place where a goto can be acceptable: in a switch statement, to jump from the end of one case to another case.
0
 

Expert Comment

by:Azmodan
ID: 8261577
if u want to get rid of an object:

obj.Dispose();
obj=null;
GC.Collect();
GC.WaitForPendingFinalizers();

if the object is not referenced in another place, it will be destroyed.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8261965
Azmodan, while this is correct, it is NOT suggested behviour since running the garbage collection is fairly expensive (in the means of CPU resources). Just as a sidenote.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question