Welcome!

Java IoT Authors: Elizabeth White, Pat Romanski, Yeshim Deniz, Liz McMillan, Frank Lupo

Related Topics: Java IoT

Java IoT: Article

Know Your Worst Friend, the Garbage Collector

It can make or break performance

The first one is called incremental garbage collection. As its name suggests, this mode does a bit of major collecting every time a minor collection is run. Global performance suffers from this execution mode but you get rid of those nasty pauses caused by major collections. Incremental garbage collection also tends to leave the heap in a fragmented state. So it's a good idea to limit its use to applications with long-lived objects. It can help you in a subtle way to obtain more responsive Swing UIs.

The second mode is a parallel GC using many threads at once, at least one per processor, to do minor collections. If your target computer has less than three processors, it is unlikely you will ever notice a difference. This option is particularly well suited to heavy servers running applications spawning many short-lived objects like web servers.

Finally yet importantly, a concurrent GC can do incremental major collection without interrupting the application for long periods of time. A concurrent GC can also use parallel execution for minor collections. According to Sun Microsystems, this execution mode gets good results with interactive applications running on a single-processor computer. To choose among these three modes and set them up, you need to use the following command-line flags:

  • -Xincgc activates incremental garbage collection.
  • -XX:+UseParallelGC activates the parallel GC. The number of threads used to do minor collections is defined by the -XX:ParallelGCThreads=<a number> flag.
  • -XX:+UseConcMarkSweepGC activates the concurrent GC. Parallel minor collections can be activated as well by using the -XX:+UseParNewGC flag.
To measure the impact of your choice on your application, you'll want to use the –verbose:gc command-line flag. You'll then get a GC activity trace on the standard output. Redirecting the standard output to a file and running the following Python script will let you draw very useful charts as shown in Figure 2.

This script can be used by specifying a file name as the first parameter or by piping it directly to the Java command.


#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-

import fileinput
import re

print "%s\t%s\t%s\t%s" % ("Minor", "Major", "Alive", "Freed")

for line in fileinput.input():
match = re.match("\[(Full )?GC (\d+)K->(\d+)K\((\d+)K\), ([^ ]+) secs]", line)
if match is not None:
minor = match.group(1) == "Full " and "0.0" or match.group(5)
major = match.group(1) == "Full " and match.group(5) or "0.0"
alive = match.group(3)
freed = int(match.group(2)) - int(alive)
print "%s\t%s\t%s\t%s" % (minor, major, alive, freed)

The result is a CSV document using tabs as separators and containing four columns of data. The first one gives the time consumed by the GC to do a minor collection, the second one gives the same information for a major collection, the third one indicates the amount of memory used by the application after the execution of the GC and the last one gives the amount of memory reclaimed by the last collection. (This script was inspired by an AWK script written by Ken Gottry in the article Pick up performance with generational garbage collection published on javaworld.com.)

The chart presents the running time of each collection performed by the GC during the application life cycle. This chart was generated with the following command line and OpenOffice.org:


$ java -verbose:gc -Xms64m -Xmx128m -XX:NewRatio=2 -XX:+UseConcMarkSweepGC -jar ../lib/jext-5.0.jar > gclog
$ gclog2csv.py gclog > gclog.csv

Although simple, this small script can help you tune your application pretty quickly. As of J2SE 5.0, Sun Microsystems provides a more powerful tool called JConsole that will graphically monitor the state of the heap and the activity of the GC as shown in Figure 3.

Further Reading

  • Java HotSpot VM Options, Sun Microsystems, http://java.sun.com/docs/hotspot/VMOptions.html
  • A Collection of JVM Options, Joseph Mocker, http://blogs.sun.com/roller/resources/watt/jvm-options-list.html
  • Pick up performance with generational garbage collection, Ken Gottry, www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc-p4.html
  • Garbage Collection in the Java HotSpot Virtual Machine, Tony Printezis, www.devx.com/Java/Article/21977?trk=DXRSS_JAVA
  • Garbage Collection Performance, Brian Goetz, www-106.ibm.com/developerworks/java/library/j-jtp01274.html
  • Using JConsole to Monitor Applications, Mandy Chung, http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
  • Comments (1) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    Suresh 05/31/05 01:44:21 PM EDT

    cannot go to page2!!!!

    @ThingsExpo Stories
    SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
    SYS-CON Events announced today that Taica will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Taica manufacturers Alpha-GEL brand silicone components and materials, which maintain outstanding performance over a wide temperature range -40C to +200C. For more information, visit http://www.taica.co.jp/english/.
    SYS-CON Events announced today that TidalScale, a leading provider of systems and services, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale has been involved in shaping the computing landscape. They've designed, developed and deployed some of the most important and successful systems and services in the history of the computing industry - internet, Ethernet, operating s...
    SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
    SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
    SYS-CON Events announced today that TidalScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...
    Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
    Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
    Infoblox delivers Actionable Network Intelligence to enterprise, government, and service provider customers around the world. They are the industry leader in DNS, DHCP, and IP address management, the category known as DDI. We empower thousands of organizations to control and secure their networks from the core-enabling them to increase efficiency and visibility, improve customer service, and meet compliance requirements.
    As popularity of the smart home is growing and continues to go mainstream, technological factors play a greater role. The IoT protocol houses the interoperability battery consumption, security, and configuration of a smart home device, and it can be difficult for companies to choose the right kind for their product. For both DIY and professionally installed smart homes, developers need to consider each of these elements for their product to be successful in the market and current smart homes.
    With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
    Most technology leaders, contemporary and from the hardware era, are reshaping their businesses to do software. They hope to capture value from emerging technologies such as IoT, SDN, and AI. Ultimately, irrespective of the vertical, it is about deriving value from independent software applications participating in an ecosystem as one comprehensive solution. In his session at @ThingsExpo, Kausik Sridhar, founder and CTO of Pulzze Systems, will discuss how given the magnitude of today's applicati...
    Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
    SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
    Digital transformation is changing the face of business. The IDC predicts that enterprises will commit to a massive new scale of digital transformation, to stake out leadership positions in the "digital transformation economy." Accordingly, attendees at the upcoming Cloud Expo | @ThingsExpo at the Santa Clara Convention Center in Santa Clara, CA, Oct 31-Nov 2, will find fresh new content in a new track called Enterprise Cloud & Digital Transformation.
    SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp emp...
    In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It’s clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Tha...
    SYS-CON Events announced today that Avere Systems, a leading provider of enterprise storage for the hybrid cloud, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere delivers a more modern architectural approach to storage that doesn't require the overprovisioning of storage capacity to achieve performance, overspending on expensive storage media for inactive data or the overbui...
    SYS-CON Events announced today that Avere Systems, a leading provider of hybrid cloud enablement solutions, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere Systems was created by file systems experts determined to reinvent storage by changing the way enterprises thought about and bought storage resources. With decades of experience behind the company’s founders, Avere got its ...
    Amazon is pursuing new markets and disrupting industries at an incredible pace. Almost every industry seems to be in its crosshairs. Companies and industries that once thought they were safe are now worried about being “Amazoned.”. The new watch word should be “Be afraid. Be very afraid.” In his session 21st Cloud Expo, Chris Kocher, a co-founder of Grey Heron, will address questions such as: What new areas is Amazon disrupting? How are they doing this? Where are they likely to go? What are th...