I've been thinking to run a script like yours, but for
ps -ax -o "rss,wq,command" 164
Process 164, at present, is loginwindow, which has been suddenly exploding in memory size, from ~25mb to 10GB. I tracked down a cache belonging to it, nuked it (which caused it to crash on logout, providing me with more useful information). I suspect that the use of a specific app is causing the memory bloat.
In your case, I suggest installing iStat Menus and keeping an eye on the memory in your menu bar. Then you'll be able to see which process is exploding. For instance, I found a serious bug in Final Cut Pro where, after a micro SD card has been inserted, FCP will start filling up memory until the computer crashes, pretty much. I can't help wondering if something like that is happening on your computer.