Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

__gc vs. __value

Status
Not open for further replies.

vladibo

Programmer
Sep 14, 2003
161
0
0
CA
I am new to .NET C++ and I would like to ask the question about usage of __gc classes versus __value.

When shall I use __gc and when __value. I understand that in __value I will have more trables to insure all unused objects are deleted but in practice when shall I use __gc if I want my program to work quickly?

And one more question. Is C++ with garbage collector working at the same level of efficiency as C# or Java?

Thank you in advanece
 
Hello

the garbage collector is meant to free the programmer from memory issues, of course; it must work under the CLR. If you want your memory to be managed by the GC you need to move it to the managed heap; all .net type that are under GC are in the managed heap, and GC occurs when unused memory on the heap is insufficient. As long as you are using the .net framework you will probably be using the GC, unless you specify 'unsafe' code (c#) or for c++.net did not specify __gc.

Garbage collection is expensive and this is why value types, which are 'small and short-lived' are allocated to runtime stack.

An alternative is to set the /clr tag to bring your old c++ code immediately to managed heap ('It Just Works'), but this may conflict with other options, runtime checks /RTC for example, will not work with /clr.

If you are not dealing with real-time programs the gc is probably what you need. 'Working quickly'- not too sure what you mean.

Allocation of resources in managed heap is comparable to unmanaged, but its the freeing of resources that is the problem; using gc you do not know when the GC is going to take place. Also, using gc multiple inheritances are not allowed. There are other issues... may I recommend Visual C++.NET: A Primer for C++ developers? Wrox press Ltd.
 
OK, but what is I use __gc and at the same time I try to use
Code:
delete myObject;
whenever I can? Will it improve the speed?
 
you can define destructors and use delete, but the memory is still not freed until garbage collection.

you can in fact force garbage collection, but as previously mentioned, GC is costly...

delete YourClass;
System::GC::Collect();

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top