?
Solved

memory management

Posted on 2003-03-30
9
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 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
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!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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