<p>i know that i can get stacktrace by using Thread.getAllStackTraces()(it returns Map, but clear does not work). When I run recursion method i can get exception because of stacktrace being too big, is there any way to clear it?</p>

It's not the stack trace that is causing your problem, it's the stack itself.  Every time you call a method, information gets put on the stack that allows you to return to the calling method.  If you recurse enough, eventually you fill all the space on the stack (which is bound by a maximum size) and your program stops.  The stack trace simply shows you your history of calls.  You probably have an infinite recursion going on and need to find a way to stop it before your program fails.  Either there is an error in your algorithm or you need to find a way to solve it differently within the bounds of the memory available to you.