Code Monkey home page Code Monkey logo

java-snippets's Introduction

Java Snippets

Snippets of useful/interesting Java code

Cool Libraries

Ideas

  • Sorting (plus searching / binary search)
  • Divide-and-conquer
  • Dynamic programming / memoization
  • Greediness
  • Recursion
  • Stack
  • Queue
  • Hashset
  • Hashtable
  • Binary tree
  • Heap
  • Graph
  • Heaps, list, queues, stacks, btree, db algorithms
  • Algorithm complexity
  • Tress - binary, n-ary, trie, balanced binary tree such as red/black tree, sply, AVL
  • Tree traversal - BFS, DFS, inorder/postorder/preorder
  • Graphs - objects, points, matrix, Djkstra, A*
  • NP complete problems - travelling salesman, knapsack problem
  • Maths - combinatarics, probabilty, n-choose-k problems, discrete maths
  • Selection sort, heapsort, mergesort, quicksort, binary search
  • Adjaceny matrix, adjacncy list
  • Mutex, semaphore, deadlock, livelock, lock/monitor

Garbage Collection Tuning

Some of my favorite articles on the subject include (if you are new to GC tuning you should read these first):

The young and old generation use different types of algorithms for garbage collection. The young generation uses a copying collection algorithm that moves all the live object from one area to another, leaving the dead objects behind. The old generation uses a mark-and-sweep collection algorithm. Copy collection time is roughly proportional to the number of live objects, mark-and-sweep collection is roughly proportional to the size of the heap. This is why the young heap is small and collected frequently and the old heap is big and collected less frequently.

There are two major aspects to play with: the algorithm used (on both the young and old generation) and the amount of memory allocated.

Algorithm

On a multicore machine the choice is between the parallel algorithm and the concurrent algorithm. Parallel means that during the stop-the-world pause the collector uses multiple threads to complete the job. Concurrent means that some of the work can be done whilst the application is running, therefore reducing the length of the stop-the-world pause. The important difference is:

  • Parallel: use when optimizing for throughput, therefore your system will be able to process more requests, but stop-the-world pauses will be more noticeable.
  • Concurrent: use when optimizing for latency, therefore your system will produce more consistent, but slower results.

On all the systems I've worked on, consistency is more important so I normally use the concurrent algorithm. The concurrent algorithm only works on the old generation, but the parallel algorithm will be defaulted for the new generation.

ParNew collects the new generation. It is a copying collector which uses multiple GC threads. Concurrent Mark Sweep collects the tenured generation. Performs the following phases:

  • Initial Mark (stop the world)
  • Concurrent Marking
  • Remark (stop the world)
  • Concurrent Sweep
  • Resetting

To enable the concurrent algorithm use the flag:

-XX:+UseConcMarkSweepGC

To enable the parallel algorithm I use the flags:

-XX:+UseParallelGC -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy

Memory

Increasing the memory allocated will cause longer GC, but also mean less frequent runs. I usually find that allocating the minimum amount is usually the best strategy, because even though more GC collections will occur, they will be much faster. The start size and max size should be set to the same size:

-Xmx512m -Xms512m

Debug Flags

I like to set the following:

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/home/user/gc.log

To get even more details I add:

-XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC

To check if a flag is set by default:

java -XX:+PrintFlagsFinal | grep FLAG

Random example:

java -XX:+PrintFlagsFinal -version

-Xms8g
-Xmx8g
-XX:NewSize=6g
-XX:SurvivorRatio=20
-XX:+UseConcMarkSweepGC
-verbose:gc
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCTimeStamps
-XX:+PrintTenuringDistribution
-Xloggc:/var/log/uni/gc.log

java-snippets's People

Contributors

benblack86 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.