Recent Post

Categories

Archives

Cow Computing

09 Sep 23

Java – Cloning an InputStream

There’s a moment when you have been given an InputStream, with its markSupported returned “False”. This means you could only read the stream one and only once, as you can’t call “reset” on it. So, what to do if you want to read the stream for more than one time and be able to re-read the previously read data. The answer is “CLONE”. My very own approach would be to use Apache Commons to turn the stream into string in which it acts as a base. Whenever i need that InputStream, i turns the string back into an stream without affecting the read position and data lost. OK, here’s the steps.

Suppose you got a request object with InputStream.

// request is the Request Object
// Turns InputStream into string
String base = IOUtils.toString(request.getInputStream());

// Whenever you need the InputStream
// Turn the string back to InputStream this way
InputStream in = new ByteArrayInputStream(base.getBytes());

09 Sep 6

JAVA Substring Bug!

Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring returns the part of the original string rather than a new one. Thus, GC wont collect the original string. So when the file or string get larger and larger, it’s actually taking up more and more memory, and thus OutOfMemory Error. To solve this, there’s nice workaround which is simple.

String newString = new String( oldString.subString(0, 5) );

Simple, Right?!

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.