Here is an example of do-it-yourself memory profiling e.g. showing used heap memory over the life of a program run. I compared it with results from YourKit to convince myself it's ok.
The concept:
- Launch a separate thread that collects used memory stats
- add a shutdown hook to output the stats