views:

63

answers:

3

.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?

+2  A: 

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

Anton Gogolev
A: 

Have you considered WeakReference ?

David B
I've actually implemented it, but didn't like it change it to custom caching. The problem to be honest I'm not quite sure where the memory leak is, maybe my strings are out of the memory just fine. Couldn't find a proper memory profiler that can profile a complex application.
dr. evil
See: http://msdn.microsoft.com/en-us/library/ms404247.aspx . As the article says, "Avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects."
MusiGenesis
@MusiGenesis that's why I changed the design :)
dr. evil
@dr. evil: I had a co-worker go through a phase a couple of years ago where he started using WeakReferences *everywhere* . I would vote for having WeakReference completely removed from .Net.
MusiGenesis
:) I see, yeah I actually quite tempted by their design but in practice they not that cool :D
dr. evil
A: 

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.

MusiGenesis
I tried to track them down, so far no luck in that department :)
dr. evil