Recent Post

Categories

Archives

Cow Computing

09 Sep 6

Garbage Collection in Java

Unlike C/C++ or older languages, Java helps user in managing their memory and auto release the unused memory.
This process is named “Garbage Collection” (GC). In brief, GC consists of three steps:

1. Mark — Find all the unreachable memory, null-ed variables’ memory and mark.
2. Sweep — Clean-up all those marked memory.
3. Compact — Rearrange the data in the memory so as to provide more constiguous spaces.

In Java, there provides 6 algorithm of GC:

For Young Sections:

1. Plain copying (DEFAULT)
2. Parallel copying
3. Parallel scavenging

For Old Memory Sections:

1. Plain mark-sweep (DEFAULT)
2. Incremental collector
3. Concurrent mark-sweep

(*note that, the heap managed by java divides into three main area: Young — where they will be cleaned very soon; Old — where they will be kept for sometimes; and Perm — where they are not to be cleaned.

If ones’ program is very computational demanding or memory draining, one could inspect and pick the most suitable GC Algorithm.
Profiling Tool

There are actually tools bundled with JDK to help us identify the memory usage and performance of a program.
They are jStat and jConsole (Only bundled with JDK5 or above).

1. To use jStat, you need to provide certain parameter to make it works.

jStat -gc -t <vmid> <interval>

vmid is the GUID for Virtual Machine, while interval states the time between each polling.
*One could make use of jps to locate the JAVA program’s vmid.

2. To use jConsole, it’s simple. Its GUI will lead you thru.

09 Sep 6

How to Write Better Code

1. Use Descriptive Variable Names.

Never ever try to use meaningless or single-character name for variables. This could mess up the whole class. Even when doing temporary storage, we should stick to some kind of rules like, having a prefix of “tmp” followed by meaningful name.

2. Remove unnecessary header

Not only will this save the time of code compilation but also reduce the ambiguity when using certain implementation type.

3. When reusing code, NO CUT & PASTE

There’s always moments when we want to perform some sort of operation that has been coded before. DON’T COPY & PASTE. Rather, we should create a function to encapsulate them, and reuse it. This does not only reduce lines of code, but also provide more clean and generic approach on refactoring code which could ease the trouble of test-case writing.

4. Know what you are CATCHING

Catching exception has been widely adopted in java and it’s sort of compulsory. In CECID’s code base, this is true as well. So one important thing to note is that, we should understand what we are catching when writing “catch”. Often eclipse and other IDE will generate codes for you. But, do you know why? Always include useful error code/warning rather than empty catch blocks.

5. Don’t retain something useless

Often functions are written to return value. However, this does not necessary mean you MUST store that return value. Sometimes, in certain circumstances, return value are useless, don’t keep them, it could create confusion. When you keep them, i would expect it to be used somewhere.

09 Sep 6

Say Hello World to “Cow Computing”

This blog is named after Cloud Computing. It shows my deep interest concerning computing cloud and database.

“Cow Computing” shall be a blog to share my programming experience and my superficial knowledge on computer cloud.

Anyway, it’s a good start and hopefully, i wont be too lazy and could have my blog updated regularly.