dr. evil
dr. evil

Reputation: 27265

Clearing up large fields from memory in long lived objects

.NET 3.5, I've got some classes which stores up to 1MB of strings. Even though I need the object for a really long time I don't need to store the string for a long time.

How can I truly remove the string from memory without disposing the parent object.

Is it a good practice to use "myString = null" in this case? or shall wrap it in a private dsposable class or something?

Upvotes: 1

Views: 227

Answers (3)

MusiGenesis
MusiGenesis

Reputation: 75296

It's possible that you're referencing or copying the string somewhere else in the program (like in a TextBox?), which is keeping it alive in memory.

Upvotes: 0

Amy B
Amy B

Reputation: 110111

Have you considered WeakReference ?

Upvotes: 0

Anton Gogolev
Anton Gogolev

Reputation: 115731

IDisposable has nothing to do with memory management. Assigning null to a private variable will do just fine. And see if Flyweight fits you.

Upvotes: 2

Related Questions