Can you try, save all the hidden vectors in a list, and use detach_() to detach those vectors which are out of K steps.
It may not free the all the memory, but I guess it will prevent from backpropogating through early steps.