title: Lucene.NET lifetime management
body: <p>Let's assume that I have a basic understanding of adding and searching documents.</p>
  
  <p>What would be the best practice for managing instances of IndexWriter and IndexReader?</p>
  
  <p>Currently, my application creates a singleton instance of an IndexWriter. When ever I need to do a search, I just create an IndexSearcher from the IndexWriter by using the following</p>
  
  <pre><code>var searcher = new IndexSearcher(writer.GetReader())
  </code></pre>
  
  <p>I am doing this because creating a new IndexReader causes the index to get loaded into memory, and then waits for the GC to reallocate the memory. This was causing out of memory errors.</p>
  
  <p>Is this current implementation considered ideal? This implementation has solved the memory issue, but there is an issue with the write.lock file always existing (because IndexWriter is always instantied and opened). Here is the stack trace of the errors I get in the app.</p>
  
  <blockquote>
    <p>Lock obtain timed out:
    NativeFSLock@C:\inetpub\wwwroot\htdocs_beta\App_Data\products3\write.lock:
    System.IO.IOException: The process
    cannot access the file
    'C:\inetpub\wwwroot\htdocs_beta\App_Data\products3\write.lock'
    because it is being used by another
    process. at
    System.IO.__Error.WinIOError(Int32
    errorCode, String maybeFullPath) at
    System.IO.FileStream.Init(String path,
    FileMode mode, FileAccess access,
    Int32 rights, Boolean useRights,
    FileShare share, Int32 bufferSize,
    FileOptions options,
    SECURITY_ATTRIBUTES secAttrs, String
    msgPath, Boolean bFromProxy, Boolean
    useLongPath) at
    System.IO.FileStream..ctor(String
    path, FileMode mode, FileAccess
    access) at
    Lucene.Net.Store.NativeFSLock.Obtain()</p>
  </blockquote>
  
  <p>I'm thinking maybe it would be best to create a singleton instance of IndexSearcher for searching, and then create an IndexWriter as needed in memory. That way, the write.lock file will be created/deleted when updating the index. The only issue I see with this is that the IndexSearcher instance will become outdated, I would need to have a task running that reloads the IndexSearcher if the index has been updates.</p>
  
  <p>What do you think?</p>
  
  <p>How do you handle a large index with live updating?</p>