Best way of Excel filtering

What is the best way of filtering the data in excel? I have an excel file having more than 5k records. when I am trying to filter the records, it hangs the system and finally leads to a heap dump. But it works fine when I run with around 2K records.

@shivangee_sunX does it work fine when you filter the data in the file manually (not is the script)?

@ashapkina Yes, even the same code worked for 5K records. but now having issues. i see in the error detils as below:
unhandled event loop exception java heap space.

@ashapkina, Any updates on this issue?

@shivangee_sunX please see 2 possible solutions in this topic