tag:blogger.com,1999:blog-84051077608074329732024-02-10T13:50:22.984-08:00Java Concurrency (&c)Jeremy Manson's blog, which goes into great detail either about concurrency in Java, or anything else that the author happens to feel is interesting or relevant to the target audience.Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.comBlogger59125tag:blogger.com,1999:blog-8405107760807432973.post-11631491771826606452015-12-18T11:47:00.006-08:002015-12-18T11:48:19.260-08:00Why I don't like System.gc()I wrote this for internal-to-my-company consumption, but I couldn't find a reasonable place to put it. And then I remembered I had a blog.
The question to me was whether is ever made sense to do a System.gc(), and, more specifically, whether it made sense to do System.gc() followed by System.runFinalization().
I can't claim it is always wrong. It suffers from what some people call the Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com10tag:blogger.com,1999:blog-8405107760807432973.post-12273365535269186792013-07-09T13:55:00.002-07:002013-07-26T10:19:41.722-07:00Lightweight Asynchronous Sampling ProfilerI finally got off of my lazy elbows and wrote a proof-of-concept sampling profiler for public consumption based on the Hotspot JVM's AsyncGetCallTrace interface.
The benefit of using this interface to write a profiler is that it can gather stack traces asynchronously. This avoids the inaccuracies of only being able to profile code at safe points, and the overhead of having to stop the JVM to Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com28tag:blogger.com,1999:blog-8405107760807432973.post-24024144596784097672012-02-07T23:15:00.000-08:002012-02-08T13:26:53.892-08:00Transactional Hardware on x86While I'm posting anyway, I might as well mention the big concurrency news of the day: the new transactional memory specification from Intel. Transactional memory sounds a lot like what it is. Your code begins a transaction and continues to execute it until it ends, performing some memory writes along the way. If there are no conflicting transactions (i.e., transactions that write to the same Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com11tag:blogger.com,1999:blog-8405107760807432973.post-46327902890640051962012-02-06T22:38:00.000-08:002012-02-06T22:50:01.021-08:00Update to the Allocation InstrumenterA couple of years ago, I open-sourced a tool that lets you instrument all of the allocations in your Java program. Some quick updates today:It now supports JDK7.You can now optionally use it to instrument constructors of a particular type, instead of allocations. So, if you wanted to find out where your Threads were being instantiated, you could instrument them with a callback that invokes Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com6tag:blogger.com,1999:blog-8405107760807432973.post-54596352078709335312011-06-03T08:14:00.000-07:002011-06-13T21:55:48.409-07:00Scala / Java ShootoutI should probably comment on this paper from my colleague Robert Hundt, where he writes the same benchmark in Java, Scala, Go, Python and C++ (TL;DR here). Short version: Initially, C++ was faster than Scala, and Scala was faster than Java. I volunteered to make some improvements, which ended up with the C++ and Java implementations being about the same speed. Some C++ hackers then went and Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com36tag:blogger.com,1999:blog-8405107760807432973.post-7148349878737392952011-05-13T09:40:00.000-07:002011-05-13T09:42:06.217-07:00Java Puzzlers@Google I/OJosh Bloch and I gave one of his Java Puzzlers talk at Google I/O this year. If you hate Java, you can waste a perfectly good hour listening to us make unfunny jokes at its expense.Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com2tag:blogger.com,1999:blog-8405107760807432973.post-86015468063880869912011-04-05T16:25:00.000-07:002011-04-05T20:07:56.022-07:00Cliff Click in "A JVM Does What?"Cliff Click gave a very good talk at Google last week. For your viewing enjoyment:The slides are available here.(For those who watched it: The inventors of bytecode were in the audience. I think he was just used to making that joke elsewhere.)Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com7tag:blogger.com,1999:blog-8405107760807432973.post-86952718576144113812010-08-27T12:31:00.000-07:002011-04-06T08:38:47.438-07:00JavaOne Talk CancelledBecause I am a Google employee, my talks are not happening. Apologies to any interested parties.I'm going to try to give the talks in another venue, or at least to use the blog as a venue for some of the topics I wanted to cover.Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com3tag:blogger.com,1999:blog-8405107760807432973.post-76264607368103590382010-08-02T00:07:00.000-07:002010-08-02T15:59:58.815-07:00Why Many Profilers have Serious Problems (More on Profiling with Signals)This is a followon to a blog post I made in 2007 about using signal handling to profile Java apps. I mentioned in another post why this might be a good idea, but I wanted to expand on the theme.Hiroshi Yamauchi and I are giving a talk at this year's JavaOne. Part of this talk will be about our experiences writing profilers at Google, and in preparing for it, I realized my previous entry on thisJeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com16tag:blogger.com,1999:blog-8405107760807432973.post-47475036883327123452010-02-07T23:56:00.000-08:002010-02-08T00:30:49.098-08:00Garbage Collection, [Soft]References, Finalizers and the Memory Model (Part 2)In which I ramble on at greater length about finalizers and multithreading, as well as applying that discussion to SoftReferences. Read Part 1 first. It explains why finalizers are more multithreaded than you think, and why they, like all multithreaded code, aren't necessarily going to do what you think they should do.I mentioned in the last entry that a co-worker asked me a question about whenJeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com4tag:blogger.com,1999:blog-8405107760807432973.post-24890861523888661132010-01-31T23:39:00.000-08:002010-02-03T14:24:17.333-08:00Garbage Collection, References, Finalizers and the Memory Model (Part 1)A little while ago, I got asked a question about when an object is allowed to be collected. It turns out that objects can be collected sooner than you think. In this entry, I'll talk a little about that.When we were formulating the memory model, this question came up with finalizers. Finalizers run in separate threads (usually they run in a dedicated finalizer thread). As a result, we had toJeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com18tag:blogger.com,1999:blog-8405107760807432973.post-66929014840209646092010-01-06T20:40:00.000-08:002010-01-06T21:00:21.497-08:00A Note on the Thread (Un)safety of Format ClassesI recently got a note on another blog post asking this question:I have a general question on the thread safety and this is not directly related with your blog. I would appreciate if you could post it on your blog. I have a class that has only one static method that accepts two string parameters and does some operation locally (as shown below). Do I need to synchronize this method?public class A {Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com13tag:blogger.com,1999:blog-8405107760807432973.post-24285412337337680182009-12-10T15:52:00.001-08:002009-12-14T11:26:55.635-08:00Allocation Instrumenter for JavaIn brief: We've open sourced a tool that allows you to provide a callback every time your program performs an allocation. The Java Allocation Instrumenter can be found here. Give it a whirl, if you are interested.One thing that crops up a lot at my employer is the need to take an action on every allocation. This can happen in a lot of different contexts:The programmer has a task, and wants to Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com27tag:blogger.com,1999:blog-8405107760807432973.post-76206699170686549862009-09-07T22:56:00.000-07:002010-08-11T16:56:35.337-07:00Cliff Click on Java-vs-C PerformanceCliff Click has a terrific post on Java-vs-native-code performance. Recommended.Cliff is the Chief JVM Architect at Azul Systems, was the lead designer behind Hotspot's server JIT compiler, and is an all-around smart guy. His main drawback is that he (apparently) gets dragged into flamewars on YouTube's comment section.Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com7tag:blogger.com,1999:blog-8405107760807432973.post-44349500484336361022009-07-07T23:23:00.000-07:002010-07-02T00:28:29.981-07:00How Hotspot Decides to Clear SoftReferencesI got asked about this twice in one day, and I didn't know the answer, so I sat down and puzzled it out a bit.A SoftReference is a reference that the garbage collector can decide to clear if it is the only reference left to an object, and the GC decides (through some undefined process) that there is enough memory pressure to warrant clearing it. SoftReferences are generally used to implement Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com22tag:blogger.com,1999:blog-8405107760807432973.post-58253849446813553152009-06-17T10:30:00.000-07:002009-06-22T20:28:38.082-07:00Volatile Arrays in JavaI get asked a lot about how the volatile keyword interacts with arrays, so it is probably worth a blog post on the subject.Those of you who have read my posts on volatile (Volatile Fields and Synchronization, Volatile Does Not Mean Atomic and, most importantly, What Volatile Means in Java) will have a pretty good idea of what volatile means, but it is probably worth it to provide a reminder. Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com42tag:blogger.com,1999:blog-8405107760807432973.post-79030082559502478472009-06-09T00:38:00.000-07:002009-06-09T00:42:07.075-07:00Welcome!Mailinator's Paul Tyma linked to me. If you are following from that link, the relevant blog entry you are looking for is probably this one, specifically, the entry labeled "visibility".Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com2tag:blogger.com,1999:blog-8405107760807432973.post-72742100314604162522009-04-04T13:56:00.000-07:002009-04-05T17:13:34.670-07:00Faster Logging with Faster Logger ClassesToday, I'll discuss a little tweak I made to java.util.Logging that made my logging throughput double. I want to use it as an illustration that it often isn't very difficult to improve the performance of concurrent code by doing things that are actually pretty easy to do.So, "I" have an application that is running a couple of hundred threads on an 8-core machine, and it wants to log about 2MB a Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com19tag:blogger.com,1999:blog-8405107760807432973.post-44090283122410583692009-02-28T16:25:00.001-08:002009-03-01T01:28:58.700-08:00Small Language Changes for JDK7For those who haven't been paying attention, there is a new project from Sun to attract proposals for small language changes to JDK7. Even in the first day of open calls for proposals, the entries have been very interesting:Strings in Switch, a proposal from Joe Darcy to be able to use Strings in switch statements.Automated Resource Blocks, a proposal from Josh Bloch to be able to say things Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com34tag:blogger.com,1999:blog-8405107760807432973.post-21285518275054277462008-12-14T11:49:00.000-08:002009-12-11T11:25:46.726-08:00Date-Race-Ful Lazy Initialization for PerformanceI was asked a question about benign data races in Java this week, so I thought I would take the opportunity to discuss one of the (only) approved patterns for benign races. So, at the risk of encouraging bad behavior (don't use data races in your code!), I will discuss the canonical example of "benign races for performance improvement". Also, I'll put in another plug for Josh Bloch's new Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com53tag:blogger.com,1999:blog-8405107760807432973.post-40493199928557091832008-11-15T13:05:00.000-08:002009-06-17T10:43:02.835-07:00What Volatile Means in JavaToday, I'm going to talk about what volatile means in Java. I've sort-of covered this in other posts, such as my posting on the ++ operator, my post on double-checked locking and the like, but I've never really addressed it directly.First, you have to understand a little something about the Java memory model. I've struggled a bit over the years to explain it briefly and well. As of today, the Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com176tag:blogger.com,1999:blog-8405107760807432973.post-36739336665572442452008-11-08T23:50:00.000-08:002009-07-08T23:02:00.837-07:00G1 Garbage Collector in Latest OpenJDK DropETA: The G1 collector is now in the latest JDK6 release, which you can download from Sun here. You can enable it for testing with the flags -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC. Original post follows.For those of you who are interested in tinkering with new garbage collectors, the "Garbage-first" G1 garbage collector is in the new JDK7 drop from OpenJDK. I haven't tried it yet, but IJeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com6tag:blogger.com,1999:blog-8405107760807432973.post-74799191624366984112008-10-26T23:49:00.000-07:002008-10-27T00:39:18.162-07:00A Brief Digression into PoliticsA plea for you to vote "No" on CA Proposition 8, and maintain the rights of same-sex couples in California.I really hate using this blog as a political forum. I have a personal blog I use for that, read only by people who know me. This blog should be expressly for computer-related topics. I apologize to readers expecting me to talk about concurrency or APIs or anything else. I apologize to Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com9tag:blogger.com,1999:blog-8405107760807432973.post-90630547493638212008-08-24T01:30:00.000-07:002008-09-13T12:29:57.519-07:00Don't Use StringBuffer!One thing I have noticed among Java API users is that some don't seem to know that there is a difference between StringBuffer and StringBuilder. There is: StringBuffer is synchronized, and StringBuilder isn't. (The same is true for Vector and ArrayList, as well as Hashtable and HashMap). StringBuilder avoids extra locking operations, and therefore you should use it where possible. That's not Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com40tag:blogger.com,1999:blog-8405107760807432973.post-3110171867488367062008-08-18T00:46:00.000-07:002008-08-27T00:43:40.602-07:00Top 3 Reasons Why Constructors are WorthlessIn my last post, I made an offhand reference to the fact that object constructors are worthless in Java. I was asked why this is, so I thought I'd fill in the details.This topic is a little more well-understood by the developer community than some of the concurrency tidbits that I usually discuss. People who use dependency injection toolkits like Guice and Spring's inversion of control tend to Jeremy Mansonhttp://www.blogger.com/profile/04241094734813086257noreply@blogger.com34