Using the ImageShack XML API
If you wanted to start a free photo uploading site, but didn’t want to pay the fixed storage and bandwidth costs of Amazon’s S3 or another CDN service, you might be interested in the free ImageShack API. Currently, it allows you to upload a photo to the ImageShack service, and get back the image’s size, a photo URL, and a thumbnail URL. You can access their API by using a simple ImageShack php class:
class ImageShack
{
var $is_url = “http://www.imageshack.us/index.php”;
var $is_result = false;
var $is_result_parsed = false;
public function upload( $file )
{
// send the image to ImageShack
$ch = curl_init($this->is_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_setopt($ch, CURLOPT_POSTFIELDS, array( ‘xml’=>‘yes’, ‘fileupload’=>‘@’.$file ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( ‘Expect: ’ ));
$this->is_result = curl_exec($ch);
curl_close($ch);
// Parse the result
$this->parse();
}
public function get_image_url()
{
return $this->get( “image_link” );
}
public function get_thumb_url()
{
return $this->get( “thumb_link” );
}
public function get_done_page()
{
return $this->get( “done_page” );
}
public function get_resolution()
{
return $this->get( “resolution” );
}
public function get_size()
{
return $this->get( “filesize” );
}
private function get( $key )
{
if( !$this->is_result_parsed )
return false;
return( $this->is_result_parsed[ $key ] );
}
private function parse()
{
if (strpos($this->is_result, ‘<’.‘?xml version=”1.0″ encoding=”iso-8859-1″?>’) === false)
$this->is_result_parsed = false;
$xmlData = explode(“\n”,$this->is_result);
$xmlr = array();
foreach($xmlData as $xmlDatum){
$xmlDatum = trim($xmlDatum);
if($xmlDatum != “” && !eregi(“links”,$xmlDatum) && !eregi(“xml”,$xmlDatum)){
$xmlDatum = str_replace(“>”,“<”,$xmlDatum);
list($xmlNull,$xmlName,$xmlValue) = explode(“<”,$xmlDatum);
$xmlr[$xmlName] = $xmlValue;
}
}
$this->is_result_parsed = $xmlr;
}
}
Accepting file uploads in PHP is almost trivial. You just need a form on a web page somewhere with multipart encoding, such as:
<form enctype="multipart/form-data" id="uploadform" method="post" action="http://example.com/path/to/upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input id="anchor_file" type="file" name="files[]" />
<input type="submit" value="" id="submitimages" name="submitimages" />
</form>
On the server side, you need to iterate through the special $_FILES array in PHP:
for($i = 1; $i < count($_FILES[’files’][’name’]); $i++){
$error = $_FILES[’files’][’error’][$i];
if($error !== 0) break;$data = array(
’name’ => $_FILES[’files’][’name’][$i],
’temp’ => $_FILES[’files’][’tmp_name’][$i],
’size’ => $_FILES[’files’][’size’][$i]
);// do something with $data now, like upload it to ImageShack
}
With these bits, it’s easy to write a wraparound interface to ImageShack. In the future, I hope their API also includes being able to query for photos, comments, and ratings after uploading; at the moment I see now way to do that.
Java Memory Leaks w/ Finalize Examples
Most people think Java cannot leak memory, but actually that’s not true. Java, unlike languages such as C or Javascript, can have two kinds of memory leaks:
- True leaks - unreferenced, unrecoverable segments of memory
- Soft leaks - memory that could be garbage collected, but is “accidentally” referenced
The first kind of memory leak, the true leak, is common to C. It’s trivially easy to write a C program which leaks memory like a sieve by simple putting a call to malloc(…) inside a tight loop. This creates unbounded amounts of heap memory and eventually runs out of space. Additionally, the memory is lost if you don’t save a pointer to it. However, the same program in Java will not run out of memory:
public class finalizer {
public static void main(String[] args) {
while (true) {
finalizer f = new finalizer();
System.out.println("" + Runtime.getRuntime().freeMemory() + " bytes free!");
}
}
}
The output looks similar to this:
15757280 bytes free!
15965176 bytes free!
16274928 bytes free!
16584368 bytes free!
15770768 bytes free!
…
As you can see, since the object we create are unreferenced, the garbage collector can clean them up. Circular references are also no problem for the Java garbage collector, which uses a generational mark-and-sweep algorithm, as demonstrated by this example:
import java.util.LinkedList;
import java.util.List;
public class finalizer {
protected Object ref;
public finalizer(Object ref){
this.ref = ref;
}
public static void main(String[] args) {
while (true) {
List x = new LinkedList();
for (int i = 0; i < 100000; i++) {
x.add(new finalizer(x));
}
x = null;
System.out.println("" + Runtime.getRuntime().freeMemory() + " bytes free!");
}
}
}
This creates a linked list in which every element also contains a reference to the list, then blows away the scope of the list entirely by dereferencing x and moving into a new loop scope. This leaves all the elements we just allocated floating in memory and all pointing at each other, but not pointed to by our active memory graph. The output shows that this memory is reclaimed:
12601488 bytes free!
8597784 bytes free!
4584656 bytes free!
12596808 bytes free!
…
Unfortunately, though, there is a way to create true memory leaks in java, and that is with a poor implementation of the finalize method, which is described in the Java 6 docs as:
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
The finalize method exists on every Object and is called by the garbage collector’s thread before it reclaims that memory. So, the simplest thing we can do to prevent the garbage collector from reclaiming it is to yield execution:
public class finalizer {
@Override
protected void finalize() throws Throwable {
while (true) {
Thread.yield();
}
}
public static void main(String[] args) {
while (true) {
for (int i = 0; i < 100000; i++) {
finalizer f = new finalizer();
}
System.out.println("" + Runtime.getRuntime().freeMemory()
+ " bytes free!");
}
}
}
This produces a quick memory leak and Out of Memory Exception:
12591736 bytes free!
8599816 bytes free!
4584576 bytes free!
602496 bytes free!
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
Throwing any kind of Exception or Error from finalize() also prevents the garbage collector from reclaiming the memory:
public class finalizer {
@Override
protected void finalize() throws Throwable {
throw new Exception("x");
}
public static void main(String[] args) {
while (true) {
for (int i = 0; i < 100000; i++)
new finalizer();
System.out.println("" + Runtime.getRuntime().freeMemory()
+ " bytes free!");
}
}
}
12380920 bytes free!
8687296 bytes free!
5608880 bytes free!
1796496 bytes free!
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
Abuse of the Object.finalize(…) contract is the only way I know of in pure Java to create a true memory leak. If you widen the scope to include JNI (Java Native Interface), you can bind to an object which leaks memory in native code, but inside the process space of your Java application.
Ruby vs PHP Performance Revisited
Ignoring any of Hongli Lai’s actual code, I reran the PHP, Ruby, C++, Perl, and Python mergesort benchmarks he gave, and came up with substantially different results. Here are the versions of the programming languages I am using for the test:
- PHP - PHP 5.1.6 (cli) (built: Sep 18 2007 09:07:28)
- Ruby - ruby 1.8.5 (2007-09-24 patchlevel 114) [x86_64-linux]
- Perl - This is perl, v5.8.8 built for x86_64-linux-thread-multi
- Python - Python 2.4.4 (#1, Oct 23 2006, 13:58:18)
- C++ - gcc version 4.1.2 20070626 (Red Hat 4.1.2-13)
- Java - Java(TM) SE Runtime Environment (build 1.6.0_10-ea-b10)
You’ll notice I’m adding Java into the mix for fun. Here’s the results, over 10 runs, on an Intel Dual-core 1.80GHz machines with 2Gb of RAM currently running this website:

Lang Average Min Max PHP 8.8325 8.637 9.303 Ruby 7.2896 7.143 7.729 Perl 4.3231 4.262 4.428 Python 3.3465 3.289 3.417 C++ 0.5638 0.53 0.609 Java 0.4062 0.262 0.551
There are a couple important conclusions to note here that are significantly different than Hongli Lai’s:
- PHP is 21% slower than Ruby, not 41% as in his benchmark
- Python is 29% faster than Perl, not 17% as in his benchmark
- Java runs this 39% faster than C++, and 2100% faster than PHP
So, PHP is slower than Ruby, but not quite as slow as Hongli Lai would have you believe. Python is the fastest scripting language in this benchmark, while Java is the faster language all around, and is incredibly, incredibly fast. Maybe all of our code should start using java!
* NOTE: I am ignoring the obvious deficiencies of this micro-benchmark and just trying to reduplicate it. What I’ve found is that there are significant discrepancies between Hongli Lai’s run of the tests and my own, probably owing to slightly different versions of the components involved. Also, if I make some trivial optimizations to the loops in the PHP script, I can get it to run faster than everything but C++, in about 2.4s. Then again, just calling sort() is faster by another two orders… but still half as slow as Java’s built-in sort… and two orders slower than perl’s built-in.
LOLCode: LOLCat programming language
This advertisement for a NY-based programmer showed up on Craigslist, and I expect that more will follow:
YOU CAN HAS CHEEZEBURGER?
YOU HAS A FLAVUR?If so, you may be the right fit for this Midtown Manhattan Web Design Startup! We are a small company looking for a Senior LOLCode Developer, preferably with at least 1 month experience developing LOLapps. Please send a resume, along with links to any web-based LOLapps you have developed.
KTHXBYE
The recently invented LOL.NET has a compiler for the Microsoft CLR which you can use with MS Visual Studio to write and compile code. For example, to loop from 0 to 9 and print each number and then exit, you would write (it’s horrible, brace yourself):
HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
UP VAR!!1
VISIBLE VAR
IZ VAR BIGGER THAN 10? KTHXBYE
IM OUTTA YR LOOP
KTHXBYE
This can be compared to a normal language which might write:
void main(int argc, char *argv[]) {
for(int i=0; i < 10; i++)
printf(”%d”, i);
}
Among other things, they have an Eclipse syntax highlighter, an Erlang bytecode compiler, a Ruby interpreter, a javascript implementation, C and C++ support, and some translators into LUA, JS, and other languages. Check out the lolcode 1.2 language spec if you’re bored.
Feature or Bug?
You always wonder when you’re writing code if the undocumented part of the API which produces a bizarre side effect is a carefully thought out feature, or a bug. Well, now thanks to this lovely photo by Dratz you know:

It was a feature all along.

