Tuesday, August 18, 2015

How does garbage collection work in JVM (Question excerpt from Quora)

Answered by Li Pi

https://www.quora.com/How-does-garbage-collection-work-in-the-JVM

This actually varies depending on the JVM implementation, but I'm assuming you're talking about Oracle (Sun) Version 6. Java itself does not specify a particular method of garbage collection.

The JVM uses a form of garbage collector called a tracing collector, which essentially operates by first stopping the world, marking all root objects, or objects that are referenced directly by running threads, and following references, marking each object it hits along the way.

Java 6 implements something called a generational garbage collector—based upon the generational hypothesis assumption, which states that the majority of objects that are created are quickly discarded, and that objects that are not quickly collected are likely to be around for some time.

Based upon these assumptions, Java therefore partitions objects into two different generations, and then operates differently upon them.

Visually, the generations look like this:

(Not quite to scale)
Young Generation: This is where objects start out. It has two subgenerations:
  • Eden - Objects start out here.
  • Survivor - Objects that survive Eden end up here. There are two of these, and only one is in use at any given time. One is designated as empty, and the other as live. This switched every GC cycle.

Tenured Generation: Older objects with longer lifetimes end up here.

Java is smart enough to apply different garbage collection methods to each generation. The young generation is handled using a tracingcopyingcollector called the Parallel New Collector. This collector stops the world, but because the young generation is generally small, the pause is short.

For the young generation:

When Eden fills up, the garbage collector stops the world, then traces through the objects in the young generation, starting with those referenced immediately by a running thread.

Those that are marked or "alive" are copied over to the empty survivor space. This survivor space is then marked as "live", and Eden, along with the other survivor space, is marked as empty. This has the side effect of compacting all the objects into a single survivor space, allowing for rather efficient memory usage. If an object has been copied between the two survivor places a certain amount of times, its designated as tenured, and moved to the tenured section.

Eden will now be overwritten by new objects, and the next garbage collection cycle will proceed to use the other survivor space.

This usage of a copying collector for the young generation is fast because the vast majority of objects are very quickly destroyed, and generally, very few objects must be moved around.

For the tenured generation:

I'm going to steal this section from this blog post by Todd Lipcon,http://www.cloudera.com/blog/201... 

Tenured Generation – Concurrent Mark-Sweep

Every time the parallel new collector runs, it will tenure some objects into the tenured generation. So, of course, the old generation will eventually fill up, and we need a strategy for collecting it as well. The Concurrent-Mark-Sweep collector (CMS) is responsible for clearing dead objects in this generation.

The CMS collector operates in a series of phases. Some phases stop the world, and others run concurrently with the Java application. The major phases are:
  1. initial-mark (stops the world). In this phase, the CMS collector places amark on the rootobjects. A root object is something directly referenced from a live Thread – for example, the local variables in use by that thread. This phase is short because the number of roots is very small.
  2. concurrent-mark (concurrent). The collector now follows every pointer starting from the root objects until it has marked all live objects in the system.
  3. remark (stops the world). Since objects might have had references changed, and new objects might have been created during concurrent-mark, we need to go back and take those into account in this phase. This is short because a special data structure allows us to only inspect those objects that were modified during the prior phase.
  4. concurrent-sweep (concurrent). Now, we proceed through all objects in the heap. Any object without a mark is collected and considered free space. New objects allocated during this time are marked as they are created so that they aren’t accidentally collected.

The important things to note here are:
  • The stop-the-world phases are made to be very short. The long work of scanning the whole heap and sweeping up the dead objects happens concurrently.
  • This collector does not relocate the live objects, so free space can be spread in different chunks throughout the heap. We’ll come back to this later!

Why do most software engineers use a MacBook instead of a Windows or a Linux laptop (Question excerpt from Quora)

---- Answered by Eric Zheng

https://www.quora.com/Question-That-Contains-Assumptions-1/Why-do-most-software-engineers-use-a-MacBook-instead-of-a-Windows-or-a-Linux-laptop

Several big things coincidently happened around the same time in late last decade:

  • iOS and Android - 2007-2008
  • Amazon Web Services - 2006
  • x86 MacBook and Mac OS - 2006
  • Startup booming - post 2008-2009 recession
  • Git and GitHub - 2005 (git) and 2008 (github)

Combined together, they are why MacBook is so much more popular today than a decade ago (circa 2005):

1. Mobile app

The population of iOS and Android app developers is huge. It makes a lot of sense to develop iOS and Android apps on Mac. iPhone and Android were born around 2007/2008

2. Amazon Web Services

Public cloud, especially AWS, made it much easier to provision servers. However, AWS, the leading and dominant public cloud provider launched in2006, didn't support windows in the first a couple years. It added Windows support in EC2 in 2008 as a beta feature. In the following a couple years, as far as I remember, the developer community was still staying away from running Windows on AWS because the glitches here and there. Therefore, in order to take advantage of the cloud computing, many people had no choice but start to pick up Linux.

3. Startup

Tech startups started taking off soon after the 2008-2009 recession. One of the reasons was that AWS made it much easier to do startup. Startup prefer use AWS for cost saving and other cloud computing benefits. Startup attracted many best programmers, for many reasons including the potential huge financial return. Because of having joined startups, many people started to pick up Linux. Also, good programmers make their work platform better. An example in the history was that VB/Delphi/VC++ programmers on Windows 95/98, in their side project, created a lot of great softwares to scratch their own itches, such as to download/upload files (remember those FTP clients and HTTP downloaders?), play MP3 and video (WinAmp, ...), editing text file, etc.. Similar thing happened to OS X platform in the last several years. That is the power of developer ecosystem, which helped make Windows successful.

4. OS X on x86

In 2005, Apple announced to change OS X to x86 architecture. The transition started from 2006, when Apple released the first x86 MacBook, and completed in 2009 when OS X 10.6 was released which only supported x86. 

This switch has two major impacts:

First, Mac hardware changing to x86 allowed us to install Windows on MacBook, using Boot Camp (released in 2006). That made it a lot more comfortable for people to buy a MacBook as their only personal computer. Because prior to that, although many people loved the build quality and form factor of a MacBook, they were afraid of the software issues. For example, there were a lot of websites using ActiveX which weren't very well supported on Mac OS X. Now with dual systems, they have a way out: when necessary, they can boot into the Windows.

Second, now lots of the tools that work on Linux can be much more easily ported to Mac OS X, because the tools are mostly written for x86 architecture and now OS X is also x86. Like many others said, now OS X is a much nicer Linux workstation.

5. Git and GitHub

Because git was created by the creator of Linux, git works really well on Linux. Because of git, GitHub was possible and got popular. Because of git (2005) and GitHub (2008), it became much easier to do open source, compared to the days when we only had SourceForge, CodePlex and Google Code. When it's easy to do open source projects, more open source tools and lib are created, which leads to more applications on the platform (Linux/Mac OS).



p.s. I previously partially misunderstood what the asker meant. The asker was saying that the hardware MacBook is the most popular. S/He didn't say Mac OS X was the most popular. The asker seemed to have said that many MacBook hardware is running Windows. 

Having admitted the misunderstanding, I would like to keep my above answer, since it still explains my view of why MacBook (running Mac OS X) has become very popular among developers. 

Personally I don't see many people running Windows as the primary OS on their MacBook.

Monday, August 17, 2015

The Netflix Tech Blog: Making Netflix.com Faster

The Netflix Tech Blog: Making Netflix.com Faster: by Kristofer Baxter Simply put, performance matters. We know members want to immediately start browsing or watching their favorite content ...

Tuesday, July 7, 2015

IE problems

I. Something only works after you open the developer tool (F12)


In IE9, some functions related to console are only activated when the Developer Tool is opened.
For example, the console.dir(...) doesn't work unless the Developer Tool (F12) is open.
Tosolve this problem, we just need to use console.log(...) instead of console.dir(...). 
You can try console.dir([1,2]) and console.log([1,2]) to see the difference.

Related Link:

About the console:

Difference between console.log and console.dir:

II. Back button behavior on a page with Iframe

http://www.webdeveasy.com/back-button-behavior-on-a-page-with-an-iframe/

III. How to check whether a browser is IE 11
http://stackoverflow.com/questions/17907445/how-to-detect-ie11
if (navigator.appName == 'Microsoft Internet Explorer')
  {
    // Do something
    
  }
  else if (navigator.appName == 'Netscape')
  {
    // If you are here, it means your browser is not IE10 or IE older
    if (re.exec(ua) != null)
      // If you are here, it means your browser is IE11
  }

Monday, June 15, 2015

Git on Mac

1. Set [TAB] as auto-completion
    curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

Add the following code to file ~/.bash_profile :
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
Give the script permission to run:
chmod -X ~/.git-completion.bash

Close and open a new shell. Done!