Posts

Java Memory Model and optimisation.

Overview Many developers of multi-threaded code are familiar with the idea that different threads can have a different view of a value they are holding, this not the only reason a thread might not see a change if it is not made thread safe.  The JIT itself can play a part. Why do different threads see different values? When you have multiple threads, they will attempt to minimise how much they will interact e.g. by trying to access the same memory.  To do this they have a separate local copy e.g. in Level 1 cache.  This cache is usually eventually consistent.  I have seen short periods of between one micro-second and up to 10 milli-seconds where two threads see different values.  Eventually the thread is context switched, the cache cleared or updated.  There is no guarantee as to when this will happen but it is almost always much less than a second. How can the JIT play a part? The Java Memory Model says there is no guarantee...

Should Java be more high level or low level?

Overview Java 8 is bringing much antisipated features such as Lambda expressions, Type Annotations and Virtual Extensions . While this functionality is a) valuable, b) playing catch up with cooler languages, are these richer, higher level functionality the only area Java should be focused. What are the most widely used languages There are many ways to assess which are the most widely used languages. One index which attempts to consider a wide variety of sources is the Tiobe Index . The positions and ratings are taken from Jan 2013. Position Programming Language Ratings Age Cooler Level 1 C 17.9% older no lower (much) 2 Java 17.4% same no same 3 Objective-C 10.3% older no lower 4 C++ 9.1% older no lower 5 C# 6.2% newer yes lower (slightly) 6 PHP 5.5% older yes higher 7 (Visual) Basic 4.7% older no same? ...

Can synchronization be optimised away?

Overview There is a common misconception that because the JIT is smart and synchronization can be eliminated for an object which is only local to a method that there is no performance impact. A test comparing StringBuffer and StringBuilder These two classes do basically the same thing except one is synchronized (StringBuffer) and the other is not. It is also a class which is often used in one method to build a String.  The following test attempts to determine how much difference using one other the other can make. static String dontOptimiseAway = null; static String[] words = new String[100000]; public static void main(String... args) {     for (int i = 0; i < words.length; i++)         words[i] = Integer.toString(i);     for (int i = 0; i < 10; i++) {         dontOptimiseAway = testStringBuffer();         dontOptimiseAway = testString...

Local variables inside a loop and performance

Overview Sometimes a question comes up about how much work allocating a new local variable takes.  My feeling has always been that the code becomes optimised to the point where this cost is static i.e. done once, not each time the code is run. Recently Ishwor Gurung suggested considering moving some local variables outside a loop. I suspected it wouldn't make a difference but I had never tested to see if this was the case. The test. This is the test I ran public static void main(String... args) {     for (int i = 0; i < 10; i++) {         testInsideLoop();         testOutsideLoop();     } } private static void testInsideLoop() {     long start = System.nanoTime();     int[] counters = new int[144];     int runs = 200 * 1000;     for (int i = 0; i < runs; i++) {         i...

Object resurrection

Overview After an object which overrides finalize() is collected it is added to a finalization queue to be cleaned up after calling the finalize() method of each object.  By what happens if you resurrect the object? When is finalize called? The finalize method is called by a single threaded system task which calls this method for each object which has been collected. Note: the nodes in the finalization queue are objects also have finalize() methods notionally. Objects cannot be cleaned up until the GC after they have been finalized. Most objects (including the node in the finalization queue) don't overriden finalize() and so the GC is smart enough to detect this and not add them to the queue. These obejcts can be cleaned up immediately. If you override the method, even with an empty one, it makes a difference. What about resurrected objects? In the finalize() method, you can resurrect the object by giving making something point to it. e.g. a static collection...

Over one million page views

A some point recently I got more than one million pages views, including mirror sites. Thank you all for your interest and encouragement.

A use for overflow

There is simple, but expensive problem in cryptography is Modular Exponentiation which can be written as Calculate y = ( p ^ n ) mod ( 2 ^ 32 ) where p is a prime number and n is a large integer. Using BigInteger You can use the obvious way using BigInteger. BigInteger answer = BigInteger.valueOf(prime)                               .pow(number)                               .mod(BigInteger.valueOf(2).pow(32)); This works, but is very expensive for large numbers.  This can take minutes/hours for large number. Fortunately BigInteger has a method which is useful in the situation. Instead of generating a very large numebr only to modulus back into a smaller one, it has a combine...