13 August 2009

Java Memory Usage

Dear Lazyweb,

Suppose I am writing a large server, and my implementation language is Java. The memory requirements for the server are larger than what the Sun JVM provides by default. So, I need the configure the JVM to use more memory.

This isn't rocket science. Everybody knows about Sun's "-Xmx" flag.

Here is Sun's documentation for the -Xmx flag:

-Xmxn
Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit .... [elided since the limits are a lot higher nowadays....] Examples:

-Xmx83886080
-Xmx81920k
-Xmx80m

There are a couple of things that confuse me about this flag. First of all, take a look at what runs just fine on my server box:

$ java -Xmx1000g HelloWorld
Hello, world!

I can assure you that my server box does not have 1000GB of any sort of memory!

More confusingly, here is another test I can run:

$ java -Xmx1000g SomeLongRunningProgram &
$ java -Xmx1000g SomeLongRunningProgram &

So, now I've got two programs running on my server box that seem to expect that they will, at some point in the program's runtime, be able to allocate 1000GB each for the memory pool. In my mind, this means that not only do I not have enough memory here, but the memory that I do have on this machine is heavily oversubscribed.

This whole situation confuses the heck out of me. My background includes quite a bit of embedded systems programming, and in the embedded world systems typically allocate their memory up front and then treat this memory as if it was a precious resource. If you can't allocate the memory that you need up front, you know something is wrong right away and it needs to be fixed. You don't get this behavior with the Sun JVM "-Xmx" flag.

OK, so, given the work that I have at hand, the "-Xmx" flag does not give me the behavior that I wanted. So, I looked into this a little bit more and thought about this problem. Soon, I was focusing my attention on the "-Xms" flag. Here's the documentation for this:

-Xmsn Specifies the initial size of the memory allocation
pool. This value must be a multiple of 1024
greater than 1 MB. Append the letter k or K to
indicate kilobytes or the letter m or M to indicate
megabytes. The default value is 2MB. Examples:

-Xms6291456
-Xms6144k
-Xms6m

So, after thinking about this a little bit, I eventually arrived at the following pattern for specifying memory allocation for server applications:

java -Xms1024m -Xmx1024m MyServer

The point is, I am specifying the same values for "-Xms" and "-Xmx". This pattern ensures that the JVM tries to allocate the memory that it needs up front. It isn't hard for me to experiment with my server machine and to learn how much memory I can allocate. If I allocate too much, I know about the problem right away. My server's memory never gets "oversubscribed" either.

So, I think that this pattern of specifying the same values for both "-Xms" and "-Xmx" is a good pattern for Java-based server applications. In fact, after a few minutes of searching, I came across this page, which seems to offer the same advice:

Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the JVM.

So, my question is this: under what circumstances would it be advantageous to specify different values for "-Xms" and "-Xmx"? I don't see a lot of upside for doing this, especially since I know that writing solid code that handles out-of-memory errors is a pretty difficult thing to do.

27 July 2009

Bike snobs and genius bears, oh my!

A couple of musings, based on articles I read today.

First, when do you think that Stephen Colbert is going to take notice of Yellow-Yellow the "genius bear" and declare her to be a threat to national security?:

"She's quite talented," said Jamie Hogan, owner of BearVault, based in San Diego. "I'm an engineer, and if one genius bear can do it, sooner or later there might be two genius bears. We're trying to work on a new design that we can hopefully test on her."

I'll bet Yellow-Yellow will be breaking into nuclear storage depots next.

.....

Next, I read this article today and it piqued my interest with one question that has been bugging me for, like, forever: why do professional/fashionable cyclists position their sunglasses OVER their helmet straps? It makes no sense to me. I can think of several times in my life when I've been traveling down the road at 20+mph, ran into a bee/wasp, and "successfully" ripped my helmet off to let my angry tenant escape. So, my question is this: is it fashionable to rip off your helmet and destroy your sunglasses, all to save a bumblebee? Terminally un-cool cyclists like myself want to know know the answers to important questions like these.

24 July 2009

Electronic Trading Marketplace Is In Need of a Fairness Algorithm

I found this article to be fascinating:

The slower traders began issuing buy orders. But rather than being shown to all potential sellers at the same time, some of those orders were most likely routed to a collection of high-frequency traders for just 30 milliseconds — 0.03 seconds — in what are known as flash orders. While markets are supposed to ensure transparency by showing orders to everyone simultaneously, a loophole in regulations allows marketplaces like Nasdaq to show traders some orders ahead of everyone else in exchange for a fee.


I guess I find this article to be interesting for three reasons. First, I predicted that this would start to occur nearly a decade ago (which begs the question: why am I still working at $DAYJOB? Ugh.) Second, I find this to be academically interesting, because whenever I see a large-scale system with this many transactions, I wonder to myself how well the system is designed and tested (in my day-to-day work, I continue to see....interesting...methods of ensuring synchronization and transaction-consistency). And third, I think that the people who run these electronic markets really need to put some thought into implementing some fairness algorithms....otherwise I can't see how this situation won't devolve into investment firms turning themselves into high-speed loops that run algorithms that MAKE MONEY FAST....much to the detriment to the actual electronic marketplace.

20 July 2009

The Problem with Groups on LinkedIn

Like a lot of other people, I have signed up with LinkedIn in order to manage my professional contacts. Overall, I think that LinkedIn is a good service (as long as one carefully manages one's privacy, that is...). Overall, I would say that the vast majority of all of the email that I have received that came about as a result of my LinkedIn account has been worthwhile for me to read.

However, with the introduction of a new feature, LinkedIn has put my overall goodwill towards this service in a precarious place. This new feature is the LinkedIn "Groups" feature.

At first, LinkedIn Groups seemed like a good idea: give people who are part of the same logical group the ability to network. So, for example, alumni from a certain university can join a LinkedIn Group. Most of the people who are part of this "group" have never interacted professionally, but because they're part of a group now they can more easily connect. Okay so far....

The problem is that, as the size of the group grows, the probability that the group will grow to include people you never want to hear from also grows. Guess what? This is already starting to happen in LinkedIn Groups.

So, for example, if you join a LinkedIn Group devoted to the the university that you happened to graduate from, at some point in the future some loser/spammer can also join that group. At this point, because you are part of the same LinkedIn Group, this loser/spammer can send email to the ENTIRE group. There is apparently very little in LinkedIn to guard against this sort of abuse.

Before the introduction of the LinkedIn Groups feature, this sort of behavior wasn't really possible. I mean, you could get spammed by some co-worker who you knew at some previous job, but at this point you could just disconnect from this in-duh-vidual and be done with the problem. This isn't really possible with LinkedIn Groups -- either you're all in or your not.

I predict that LinkedIn is going to have to deal with a LOT of this sort of abuse in the future. I do hope that LinkedIn introduces some sort of moderation system, controlled by the users themselves. It'd be nice to hear that some loser got his LinkedIn account yanked because some threshhold of regular users deemed his/her mail to be spam.

In the meanwhile, the one thing that I've done to fix this problem for myself is to configure my LinkedIn preferences to send me "Group" updates no more often than once a week. This makes the spammers much easier to deal with.

26 June 2009

Netflix Prize Contest Winner?!

Wow! A team finally managed to attain a 10% improvement in the Netflix Prize Contest This is a very impressive achievement.

21 June 2009

Beauty in Engineering

In terms of beauty in engineering, do you know what I find to be beautiful?

Well, here's one thing: it's coming up with a plan to secure a particular network protocol, documenting the plan, and then implementing the plan in code that executes in several radically different environments.

It is (perversely?) pleasurable to see seemingly random bytes be transmitted onto a wire from one peer on the network destined to another very different peer on the network and to have all of these bytes be decoded and authenticated properly. The mathematics behind this stuff is extremely pretty, and making it all work in the Real World is fascinating.

The icing on the cake is knowing that all of the code is efficient and scalable, and that the code has a reasonable test harness so that there are no hidden surprises.

As a character on a TV show once said, I love it when a plan comes together.

12 June 2009

Penguins Win The Stanley Cup!

Wow. What a game! The Penguins have just won the Stanley Cup.

This was a hugely improbable event. The Penguins beat the Red Wings in Detroit, and the Wings are an incredible team oozing with all-stars. The Penguins had some ups and downs during the series, but, in general, they won the series by keeping their foot on the gas when it mattered. At the end of the series, the Wings looked beat-up, shell-shocked, and out of energy.

I can't believe that former Bruin Hal Gill is going to get his name on the cup. Denis Leary must be ranting in some bar right now, screaming "unbelievable!".