memory profiling with jvisualvm

I’ve been meaning to blog about jmap/jvisualvm for a while, but this JavaRanch question finally prompted me to do so.

My goal was to see where the memory was going in our JForum installation at JavaRanch. This was a one time thing, so I decided to see if I could do so without JProbe.   A colleague had already used Eclipse’s Memory Analyzer.  (Awesome tool by the way – it clues you right in to what it suspects are leaks.)  I decided to use jvisualvm.  Which was interesting.  While I was off exploring, someone else solved the problem and I put writing this up on the side.

I tried this locally on my machine to make sure I knew what to do before trying it on the production UNIX/Linux box.

How to run jvisualvm

  1. Get your process id:
    • Windows: JPS can be used to find your process id on windows.  At the DOS prompt, type “jps”.  My Tomcat process is called “bootstrap”  [I figured this out by stopping the process, running jps and then restarting the process running jps again.  I also had an unnamed process (Eclipse maybe?) and for jps itself.]  The JavaDoc says jps may not be supported in the future, but it works right now on Windows XP.
    • UNIX: You can simply use ps -ef | grep <whatever makes your process name identifiable>.  In my case, it was “ps -ef | grep coderanch”
  2. Run “jmap -dump:file=heap.bin.hprof 1234” [if your process was 1234].  Note this requires Java 6.
  3. Run “jvisualvm”
  4. file –> load –> choose hprof file from jmap
  5. Filter by classname – I used the package name net.jforum to narrow things down.  Which turned out to be a mistake as the memory leak was in Lucene.

or – Run” jhat -J-mx512m heap.bin.hprof”.  Thanks to this blog for helping with the jmap/jhat command.

[edited to fix typos]

how to test a local web application in blackberry simulator

Surely there must already be a tutorial on this, but I didn’t see one.  I did see this information scattered amongst various places.  So here it is in one place.

My goal

Make JavaRanch’s JForum implementation more mobile friendly.  It’s not too bad on the iPhone because the iPhone has a better browser.  On the BlackBerry, the forums are much more difficult to use than they need to be.  As a result I plan to starting out by testing a mobile stylesheet with the BlackBerry simulator.  I found an online iPhone simulator.  After the mobile stylesheet looks on the BlackBerry simulator, I can upload it to a test server and try with the iPhone one online.

Note: I didn’t install the BlackBerry Eclipse plugin because I’m not doing BlackBerry development.  If you want the plugin, I did come across a very detailed tutorial.

Steps to bring up app in BlackBerry simulator

  1. From the BlackBerry download site, download both the simulator and the MDS package.  You need the later in order for the simulator to do internet.  If you have a slow connection, note that both are large downloads.  The simulator is 82 MB and the MDS is 45 MB.  (Note to RIM: if one is already running Tomcat, it would be nice to distribute this as a web app.  Smaller download, no need to change to port and less of a memory hog.)
  2. If you are already using port 8080 (such as to run your own Tomcat server.) Open the file “..\Research In Motion\BlackBerry Email and MDS Services Simulators 4.1.2\MDS\config\rimpublic.property” and change the “WebServer.listen.port” property from 8080 to 8081.  Thank you BlackBerry message board for this tip.
  3. Launch the MDS
  4. Launch the BlackBerry Simulator
  5. Launch your local Tomcat
  6. Test away.  Note that you must use your computer name rather than localhost.  (On windows this is the %COMPUTERNAME% property.)  Thanks to ASP forum for this tip.

Actually using the simulator

Some impressions of using the simulator:

  • It defaults to “100%” view.  100% of what?  No BlackBerry is that big!  On a 1024×768 resolution, the screen and keys don’t both fit on the screen.  I immediately switched to 50% view which is about an inch longer than an actual BlackBerry.
  • Luckily the keyboard works without having to click on the simulated device keyboard.
  • All the buttons have a real keyboard shortcut which you can see when mousing over the simulated key.  This helps with frequent operations.
  • To use the scroll wheel, you have to click and drag.  I would have expected mouse gestures to work here because they are more similar to what you do on the device.  Luckily there is a keyboard shortcut for this (hold F9 and use arrows to your heart’s content.)
  • It grays out if you don’t use it for a few minutes.  As does the real BlackBerry.  This is a bit annoying when testing though.
  • Luckily you can paste in from the real system clipboard – useful for testing URLS

Now that I’ve gotten used to the simulator, the site is just as hard to use on the simulator as it is on a real BlackBerry.  Perfect!  That means it suits my needs.

Problems/error messages I encountered

Since most people look for a “problem/solution”, I’m also listing the surprises in that format.  I know that’s what I was looking for!  I didn’t think I’d need a tutorial on setting up a simple piece of software.

  • BlackBerry simulator hangs when I try to access the internet – check you have installed the MDS, turned it on and you don’t have a port conflict
  • 8080 in use when starting MDS – see step #2 and change the port #
  • 403 error accessing localhost – see step #6 and use the computer name
  • Fledge.exe is using a lot of memory – this is the blackberry simulator itself; not much you can do here

The goal

I saw just how bad things look.  There’s a few more steps before actually making it look better – like getting rid of the table based layout.  But I did make progress with the tool support!

when should I question a technical business requirement?

My other blog entry “what is a business requirement” covers what is a technical business requirement.  Some such requirements are fine as the customer really does know what he/she needs and is trying to save time.  Or the customer wants the system to be similar to another one.  (In which case that fact should be one of the requirements.)

On the other hand, sometimes it is our responsibility to question the requirement.  Here are six questions to ask to see when that is the case.  Again, many of these examples come from posts I’ve seen at JavaRanch rather than my actual customers.

1. Does it meet a business need?
“The user should see red text on a pink background.”

This is not a business need.  It’s a technical detail – and a poor one at that.  Examples of a proper business requirement would be:

  • “The user should see red text on a pink background because it’s part of an existing system that does that (or a standard.)”
  • “The disclaimer should be small, hidden and low contrast”

Whereas the provided “requirement” invites discussion as to what the actual business goal is.  It may be that the customer didn’t realize how hard red on pink is to read.

2. What is the real goal of the requirement?

“The user should be able to query by the last three letters of their name”

Huh?  There is an important requirement hidden in here – to be able to find people who have a title like ‘PHD’ at the end of the name.  However, this isn’t the best way to go about implementing it.  The real requirement is crying out for a different solution like a separate field.  Which will only get discussed if you start talking about the real requirement.

If you don’t understand what a requirement accomplishes, the real goal probably hasn’t been stated.

2a. Is it someone’s opinion on the best approach?

“Only allow two files to be processed at a time.”

This category is a variant of finding the “real goal of the requirement.”  In this case, the true requirement is performance.  Which is a technical matter and not a business requirement.  Real requirements should be:

  • “A file should be processed in X seconds”
  • “The earliest files should be processed first” (don’t parallellize everything)

3. Is it feasible?

“Remember the user has logged in on a given machine after restarting the browser or rebooting.  But don’t use cookies because I heard cookies are bad.”

It doesn’t how much the customer wants something if it can’t be done.  This example isn’t possible.  It’s time to talk to the customer about the trade-offs between remembering an identity and cookies.

4. Is it cost effective?

“Add this one field to the list” [where you are using dynamic paging/sorting in the database, that one field is from a table several joins away and you are just under your performance requirement]

You notice in this example, the relevant facts you know are significantly longer than the requirement.  It sounds like a trivial change to the customer and they may ask on a whim.  Once you tell the customer what the implications are, he/she can decide whether it should still be implemented.  Sometimes a customer wants a feature if it is cheap, but not if it is expensive or will degrade another aspect of the system.  Communication is the only way to find out which is the case.

4a.  Will it introduce unnecessary complexity or maintenance problems?

“Make a minor change to the search algorithm” [where the requested change would either goes against the existing architecture or would require a really unmaintainable hack]

A variant of the previous one with a different emphasis on the cost.  Again, it is only through sharing what you know that the customer has enough facts to make a decision.

5. Is it usable?

“Put the reset button to the left of the submit button”

If a requirement goes against how computers work or standard conventions, it’s likely to confuse the living daylights of the end users.  It’s your job to inform the customer of this and to make sure the customer truly understands the implications of this decision.  Maybe make a mockup and have the customer try it out.  Or stop random people in the hallway and see how they try to click.

6. Will it compromise security?

“Use http for processing a credit card transaction.”

I think we all know why this is bad.  Similar to usability, it’s your job to convince the customer that he/she doesn’t really want to do this.  Only in this case, it’s even more important.  Security concerns are unethical to code just “because someone asked you to.”  (And for someone who counters with the case where sharing public information isn’t an issue, that’s not a compromise of security.)

What other questions can you think of to advise others to ask?