Skip to content
illyfrancis edited this page Jan 2, 2014 · 11 revisions

Explanation of heap

And the memory allocation in 32bit OS etc. Because JVM requires a contiguous memory, in 32bit OS the max VM heap allocation is usually 1.2G to 1.6G according to the blog. Although that's that the limitation of the VM. The alternative is to use 64bit OS.

Swicth

  • -Xms : Sets the initial heap size for when the JVM starts.
  • -Xmx : Sets the maximum heap size.
  • -Xmn : Sets the size of the Young Generation.
  • -XX:PermSize : Sets the starting size of the Permanent Generation.
  • -XX:MaxPermSize : Sets the maximum size of the Permanent Generation

The Xmx and permgen space are separate. [http://rimuhosting.com/knowledgebase/linux/java/-Xmx-settings]

If you get java.lang.OutOfMemoryError: PermGen, increase pergen using XX:MaxPermSize like -XX:MaxPermSize=128m, the default is 64M.

Out of memory errors

http://www.kdgregory.com/index.php?page=java.outOfMemory

Caused by:

  • Permgen (as above), which is separate from the 'main' heap
  • Thread allocation
    • Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
  • and more refer to the blog

Garbage collection

Visual VM

Worth checking out

ThreadLocal

public class ThreadLocalExample {
    // SimpleDateFormat is not thread-safe, so give one to each thread
    private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue()
        {
            return new SimpleDateFormat("yyyyMMdd HHmm");
        }
    };

    public String formatIt(Date date)
    {
        return formatter.get().format(date);
    }
}

ThreadLocalRandom

http://javapapers.com/core-java/threadlocal/

Since Java 7.

private static final ThreadLocal localRandom =
    new ThreadLocal() {
        protected ThreadLocalRandom initialValue() {
            return new ThreadLocalRandom();
        }
};

Double brace initialization

http://www.c2.com/cgi/wiki?DoubleBraceInitialization

Java idioms

http://www.c2.com/cgi/wiki?JavaIdioms

Classpath wild cards since Java 6

Use java -classpath ./lib/* app.Main instead of java -classpath ./lib/log4j.jar:./lib/another.jar... app.Main

http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

hidden features listed in so

http://stackoverflow.com/questions/15496/hidden-features-of-java?rq=1

Clone this wiki locally