There is a causal connection between slowness and low RAM usage. 99% of sort algorithms need some or a lot of memory.
Check out free command (and its man page and all what follows from that -- hard core, a bit). In your case you can even do free -g to get a synopsis in single digit units (in giga).
findmnt can show a line for hugepages, kind of additional memory driver:
/dev/hugepages hugetlbfs ... pagesize=2M
In your case (massive math stuff, usage observations) this should be turned on. But no direct relation to your problem, more an optimizing thing.
This should all happen by itself -- mathematica wants memory, the kernel gives it, no problem, that is what it is here for. There are no other memory hungry programs around.
I don't bother checking the versions you give, LTS?. Somewhere must be the problem. Either one version of matlab or ubuntu, or your installation, has a problem. Hope that helps. Just make sure everything is "up to date".
As soon as that 8 GB of memory (say 80% of it freely "available") get used, it will also speed up.
As for swap that does not matter here - should not matter - unless you want to optimize some crazy stuff. In general, in matlab, you would not want it to make the kernel start swapping.