Li Pi’s answer to What’s the best way to avoid garbage collector pauses with big heaps in Java?

If the GC (Concurrent-Mark-Sweep) (see How does garbage collection in Java work?) is operating as expected, then the stop the world pauses should not be significant. CMS is designed to have as few stop the world pauses as possible. The Parallel New Collector will stop the world, but as long as your young generation is reasonably sized, you should be fine.

To recap, CMS operates by first stopping the world, then initially marking the root nodes, then it concurrently proceeds to trace through the rest of the objects. Memory is freed at the end of this process, but objects are never moved around, making fragmentation problematic.

However, there exist a few failure modes where CMS will be forced the stop the world for a significant amount of time:

1. Concurrent Mode Failure – This occurs when the tenured generation fills up before CMS has completed it's work. When this happens, the JVM will fall back to a stop the world garbage collection mode.

For efficiency's sake, the JVM attempts to start the garbage collection process as late as it can get away with. CMS tracks the growth in heapsize and attempts to time it's collection so that the collection ends right before the tenured generation fills up. Sometimes Java is wrong – such as if during the collection process, object tenuring rate increases dramatically.

You'll know if you're experiencing this failure mode if you see the words [CMS (concurrent mode failure): in your GC log.

If this failure mode is bugging you, simply set – XX:CMSInitiatingOccupancyFraction to a conservative value. This will tell the JVM to start garbage collection earlier, and thus, not run out of space so much.

2. Promotion Failure Due to Fragmentation:

This is the other biggie. If you see ParNew (promotion failed) in your GC log, you're experiencing this.

I'm gonna steal more content from Todd Lipcon and this blog post again: http://www.cloudera.com/blog/201…

This failure mode is a little bit more complicated. Recall that the CMS collector does not relocate objects, but simply tracks all of the separate areas of free space in the heap. As a thought experiment, imagine that I allocate 1 million objects, each 1KB, for a total usage of 1GB in a heap that is exactly 1GB. Then I free every odd-numbered object, so I have 500MB live. However, the free space will be solely made up of 1KB chunks. If I need to allocate a 2KB object, there is nowhere to put it, even though I ostensibly have 500MB of space free. This is termed memory fragmentation. No matter how early I ask the CMS collector to start, since it does not relocate objects, it cannot solve this problem!

When this problem occurs, the collector again falls back to the copying collector, which is able to compact all the objects and free up space.

Solutions:

Dealing with this failure mode is more difficult. As answerers have mentioned above, try to do things in a way that don't create garbage in the tenured generation – the new generation is always collected by a copying collector, thus fragmentation doesn't occur in the young generation.

The tenured generation makes the assumption that objects that are allocated together die together, but if we violate this tenet, fragmentation becomes a big problem. You can get around this by manually allocating memory in a way that objects next to eachother die at the same time, and can thus be collected together.

Todd Lipcon gives an awesome writeup of this approach with a Local Allocation Buffer in this blog post. http://www.cloudera.com/blog/201…

Another possible solution I'm working on at Cloudera is to move the most memory hungry elements of the application off-heap, either through the usage of DirectByteBuffers or via JNI. A slab allocation model similar to MemCached can be used in order to trade space efficiency for fragmentation overhead. 

You may either choose to manage memory manually, and copy stuff on heap when necessary, or wrap references to the external code in a phantom references, and use a reference queue to keep track of which references have been garbage collected, and then free them using some form of free(). If you just need a cache, BigMemory provides a commercial, off the shelf solution.

You're also free to rip out the allocator and cache I implemented in https://issues.apache.org/jira/b….

Obviously, both approaches employed by Todd and I are engineering intensive, and not simple to implement. But if GC tuning fails, and if you really want to minimize pauses with your Java/Scala/etc apps, then you might want to experiment with these approaches.

What's the best way to avoid garbage collector pauses with big heaps in Java?

PRK Recovery: Day 9

Not much has changed at all. No pain, some eye dryness when I wake up, but none during the day. I’ve stopped all drops except the steroid drops twice a day.

According to research I’ve done, while inflammation makes the epilithium heal faster, it also causes it to heal in a less smooth manner. So I guess I’ll be on this for a while.

This ban on physical activity is on for another 5 days, other than being bored, I’m perfectly fine.

My vision has improved from 2 days ago, though I haven’t measured it, ghosting still exists during the night. This should continue for another 5 months or so….

PRK Recovery: Day 5 and 6

Really nothing interesting to report. I tapered down on the eyedrops, no pain. My vision is again, fine, but I don’t really have very much clarity.

I’m getting my bandages taken out tomorrow. I’ll write more then.

PRK Recovery: Day 4

PRK does this thing where, your vision gets worse, then gets better. I enjoyed acceptable vision on the later parts of Day 1, and during the early parts of Day 2.

When I woke up, my vision went to pretty much useless for computer work. Its hard to describe, but I can focus okay. It’s almost as if someone took a blur filter to my eyes. I can see all large objects, but no small ones, such as text, without zooming in.

I managed to drive myself to my checkup – my right eye is fine, but my left eye is still not finished healing. I’ll have to wait another 3 days before I get the bandage contacts taken out. LVI is closed on Tuesdays and Wednesdays – so they do their PRK checkups on Day 4/7 rather than the traditional day 5.

In the afternoon, theres no pain at all. My left eye feels perfect, my right eye every once a while feels a little scratchy.

PRK Recovery: Day 3

Woke up, nothing interesting. Put eyedrops in eyes. After about 3-4 minutes, right eye starts hurting. I’m not sure how to describe it, but its not really a sharp pain, but irritating enough I can’t hold it open for than 20 second at a time.

I never got any Vicodin – so I took some aspirin. Can’t go back to sleep either.

Whatever is irritating my right eye is causing tear production to go through the roof. Its spilling out of my eyes. I was given temporary punctual plugs during the surgery, this may have not been necessary. At the very least, I won’t have any issues with dry eyes.

I’m getting my bandage contacts taken out tomorrow - I’m not sure if I’ll be able to drive. My vision is certainly sufficient, but being forced to blink every once in a while is a huge pain, and probably dangerous.

PRK Recovery: Day 2

When I woke up, everything was fine. Other than my vision being slightly blurry due to the healing process, I wasn’t in any pain. This was unexpected, as every other recovery blog out there has Day 2/3 as being the worst

But, about 6 hours later, the pain began. I can’t really open my eyes for more than 5 minutes without needing to collapse on the bed.

The people who did LASIK are perfectly fine by now. I’ll type more later once Im more able.

PRK Recovery: Day 1

I just had my surgery today at the Lasik Vision Insitute in San Diego.

I’m currently a 21 year old undergrad at UCSD. I’m in excellent physical health in every possible way except for my eyesight. This is something I’ve been contemplating ever since I became an adult, but my eyes have only been refractively stable enough for the procedure to happen.

I understand PRK is a painful procedure with long healing times;  and full recovery can take upwards of a year. My corneal thickness is well within the range for either LASIK or PRK, but I opted for PRK because it was the safer procedure. LASIK posed the risk of long term I looked into other surface ablation techniques, but studies on them don’t seem to show any benefit over PRK alone.

My procedure was a “Advanced CustomVue” ablation, and performed on a VISX Star S4 w/ Iris Registration technology. This is the most modern laser approved by the FDA, and the fact that LVI possessed one greatly reassured me.

The surgery itself was relatively uneventful. I was driven to the clinic by my roommate, and after waiting for a while, was ushered onto the surgery table. I was not given Valium or anything beforehand. Not necessary unless you’re actually panicking – merely anxious isn’t enough to justify it.

The surgeon proceeded to scrape off my epithelium after applying what I assume was an alcohol solution, then I was instructed to stare into the light. The actual lazing lasted only 28 seconds per eye.

It was over quickly, and I went into the waiting room to wait for my ride again. LVI is definitely one of those “Lasik factory” style companies, where you don’t get much personal attention from the surgeon. Still though, the surgeon is well qualified, the equipment is modern, and the prices can’t be beat.

I’m writing this post 7 hours later, and I don’t feel much pain at all. I can see reasonably well, though not as well as before my correction. I don’t have an issue with seeing small text at the moment. I’ve been told the worse is yet to come. I’ll post on that later.