Honestly, my main goal when optimising a game with 40000 sprites would be trying to think of clever ways to not have 40000 sprites.
I'm not saying you should delete stuff from your game, but there might be a smarter way to do this. For example, if you've got a giant map with 5000 tree objects on it - do you really need to have all those trees in memory given that you can only see a few of them at once? Maybe you could just have an array that remembers where the trees are, keep a stock of say 100 trees, and then just teleport them into position when necessary. Looks the same to the player, but waaay less stuff sitting around in memory.
Or: are there any sprites that you don't really need to have in memory? Eg. maybe you have a bunch of invisible spawner objects that populate the map with enemies or whatever. Well, once the spawners have done their job they can be deleted, right? No need for them to be taking up memory any more.
You can also put objects that don't move a lot (eg scenery items) onto a layer together and enable render cells for that layer. This might improve your performance, but it doesn't always - you should test.
If you let us know more details about your game, how it looks and how it works, we can make more detailed suggestions for ways you can optimise it :)