title: pkcs#11 memory error - what might be the common reasons?
body: <p>I am getting the <code>CKR_DEVICE_MEMORY</code> error code for <code>C_Encrypt</code> call using cryptoki library.</p>
  
  <p>From the PKCS#11 spec, <code>CKR_DEVICE_MEMORY</code> means The token doesnot have sufficient memory to perform the requested function.</p>
  
  <p>Under what circumstances, do we get the token's memory completely full? </p>
  
  <p>The HSM has been working 24x7 for 7 days continuously mostly encrypting and decrypting files during the day time with 2 parallel sessions. I haven't called <code>C_Finalize</code> in the last 7 days. so cryptoki library has been working in its memory space from the point it has been initialised(<a href="https://stackoverflow.com/questions/3175580/hsm-cryptoki-sessions-timeout">see a related post on this</a>).</p>
  
  <p>I can see from my applications, debug log, what ever, i am allocating, i am deallocating so there is no memory leak from my application code.</p>
  
  <p><strong>UPDATE 1:</strong> There is a <a href="https://stackoverflow.com/questions/3068464/newbiewebservice-threads-close-if-no-active-threads-if-any-activen-then">related detailed discussion</a> on how i can call <code>C_Finalize</code> in <code>Application_End</code>of the ASP.NET. The main reason i couldn't use this because after recycling/timeout, the ASP.net threads access a single session resulting in CKR_OPERATION_ACTIVE error. In my case multiple applications are accessing the HSM via a webservice.</p>