<p>During execution, how can a java program tell how much memory it is using?</p>

<p>I don't care how efficient it is!</p>

If you are have a java1.6 somewhere and your program is running in java 1.4.2, 1.5 or 1.6, you can launch a
visualVM session
, connect to your application, and follow the memory (and much more)
(The images are not displayed at the moment, so please refers to
Monitoring an application
for an illustration.)