| |

VerySource

 Forgot password?
 Register
Search
Author: tontonhu

How to release a custom object in C #?

[Copy link]

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-22 15:45:01
| Show all posts
GC.Collect; // Force garbage collection for all generations
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-22 17:00:01
| Show all posts
GC.Collect (); // Force garbage collection for all generations
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-22 19:15:01
| Show all posts
Implement the IDisposable interface, which has a Dispose method. In this method, the resources in the object are released. After using it, manually call Dispose. There are some precautions, which are explained in detail in MSDN.
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-23 00:00:01
| Show all posts
GC.Collect (); // Force garbage collection on all generations-how does the foo object GC recognize it as garbage? In fact, after I call this sentence, the foo object is still alive.

Implementing the IDisposable interface, my Foo class is very simple, there is no resource to release, only the memory occupied by its foo itself, how to release it?
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-23 01:15:01
| Show all posts
The .Dispose method is used to release unmanaged resources and has nothing to do with releasing memory ~
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-23 04:00:01
| Show all posts
//first
foo = null;
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-23 09:15:01
| Show all posts
thank you all!

foo = null;
GC.Collect ();


This answer seems to work.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list