This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Are you talking about overall heap size, or about heap usage? Either way, I don't think that such a script will do for you what you think it will. In the former case, heaps tend to increase in size as the app is used, especially with apps such as Java EE application servers. For the latter, once the heap usage reaches a certain amount, a major garbage collection will come along and free a bunch of space. You can observe both of these by hooking up either jconsole or jvisualvm to your Java app.
By the way, what is it that you are really looking for?