Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Collect garbage of object with memory leak
    primarykey
    data
    text
    <p>I am using a 3rd-party object I didn't create that over time consumes a lot of resources. This object shouldn't in any way contain a state, it simply performs a calculation. Despite this fact, everytime I call a specific function of this object a little more memory is consumed. A few hours later, and my program is sitting at gigabytes of allocated memory.</p> <p>The object was origionaly initialized as a static member of my Program class in my command-line application. I have found that if I wrap my entire program in an class, and reinitialize it every now and again, the older (and bloated) object is unallocated by GC and a new smaller object replaces it.</p> <p>My issue is this method is quite clumsy and ruins the flow of my Program. </p> <p>Is there any other way you can dispose of an object? I am lead to believe GC.Collect() will only dispose unreachable code. Is there anyway I can make an object 'unreachable'?</p> <p><strong>Edit:</strong> As requested, the code:</p> <pre><code>static ILexicon lexicon = new Lexicon(); </code></pre> <p>...</p> <pre><code>lexicon.LoadDataFromFile(@"lexicon.dat", null); </code></pre> <p>...</p> <pre><code> byte similarityScore(string w1, string w2, PartOfSpeech pos, SimilarityMeasure measure) { if (w1 == w2) return 255; if (pos != PartOfSpeech.Noun &amp;&amp; pos != PartOfSpeech.Verb) return 0; IList&lt;ILemma&gt; w1_lemmas = lexicon.FindSenses(w1, pos); IList&lt;ILemma&gt; w2_lemmas = lexicon.FindSenses(w2, pos); byte result; byte score = 0; foreach (ILemma w1_lemma in w1_lemmas) { foreach (ILemma w2_lemma in w2_lemmas) { result = (byte) (w1_lemma.GetSimilarity(w2_lemma, measure) * 255); if (result &gt; score) score = result; } } return score; } </code></pre> <p>As similarityScore is called, more memory is allocated to a private member of <code>lexicon</code>. It does not implement IDisposable and there are no obvious functions to clear the memory. The library is based on WordNet, and uses an algorithm to find path lengths in the hypernym tree to calculate the similarity of two words. Unless there is caching, I can't see why it would need to store any memory. What is for sure, is I can't change it. I'm almost certain there is nothing wrong with my code. I just need to dispose of <code>lexicon</code> when it gets too large (N.B. it takes a second or two to load the lexicon from file to memory) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload