| |

VerySource

 Forgot password?
 Register
Search
View: 1634|Reply: 16

How to release a custom object in C #?

[Copy link]

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-17 17:30:01
| Show all posts |Read mode
public class Foo
{
    public string name;
}

Foo foo = new Foo ();
foo.name = "tata";

//use

// After using it, I want to release the memory of foo in the heap immediately. How to release it?
Reply

Use magic Report

3

Threads

11

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-4-21 01:45:01
| Show all posts
Close directly

foo.Close ();
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-21 16:30:01
| Show all posts
Use the stackalloc keyword
Allocate a memory block on the stack, this memory is not subject to garbage collection, and is automatically released after the method returns
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-21 18:45:02
| Show all posts
Need unsafe context
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-21 19:45:01
| Show all posts
The memory of managed objects can only be automatically recovered by GC
Reply

Use magic Report

1

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-21 20:00:01
| Show all posts
I think the garbage collector will automatically recycle it
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-4-21 22:00:02
| Show all posts
It can be ignored, GC will automatically recycle ...
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-21 23:45:01
| Show all posts
cqgang:
How to add stackalloc? If I have to release it dynamically, is it not supported?
Reply

Use magic Report

3

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-22 10:00:02
| Show all posts
kalian2000:
I think it's up to me to decide when GC will reclaim heap memory, just to quickly end the life of that custom object, is it okay?
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-22 15:30:01
| Show all posts
stackalloc can only be used for unmanaged types
Such as
Char * pc = stackalloc Char [10];
// Because it is a pointer type, it must be placed in the Unsafe block
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