Tuesday, July 31, 2012

Garbage Collection Mechanism of Fuzuli Interpreter

Fuzuli, our programming language and interpreter, has a garbage collection utility since its early stages. Garbage collection is an old term in computer science.

A chunk of memory is allocated for each software program by operating systems. Programs also allocate memory at runtime. Those programs are responsable to free the memory they allocated. Operations for allocating and freeing memory areas are performed using single commands such like malloc, free, new and delete in C and C++.

But allocating and freeing the chunks of memory is not that easy. When a reference to a dynamically created object is broken, the object remains suspended in the memory. Look at code below:


(let a (list 5 6 10 "Text"))
(let a NULL)

In the code above, a list of 5, 6, 10 and Text is created and referenced by the variable 'a'. Then, a is set to NULL. After all, what happened to list and its objects? The answer is easy. They suspended in the memory and waiting to be cleaned.

Ok, what about the code given below?


(let b 11)
(let a (list 5 6 10 b))
(let a NULL)


In the code above, a is linked to a list which contains 5,6,10 and b. b is an other variable which has a value of 11. After setting the value of 'a' to NULL, there is some garbage but this is a little bit different. Cleaning the object referenced by 'a' also means cleaning the object referenced by b. But we don't 'b' to be cleaned, it should stay alive. Reference Counting now comes into account. Counting references given to an object gives more information about the aliveness status of an object. In this example, the integer object has only one references in (let b 11).

When the code (let a (list 5 6 10 b)) runs; references of objects 5, 6, 10 and b increased by 1. The old reference count of b was 1, so b has a reference counting of 2. When (let a NULL) runs; reference counts of all objects contained by 'a' are decreased by 1. After all, the object which have reference count of 0 are deleted from the memory. The object 'b' is still alive!. Fuzuli uses this mechanism.

Garbage collecting in Fuzuli is automatic by default. Calling


(gc false)


simply disables the automatic garbage collector. Calling

(gc true)


enables the garbage collector. Whenever the garbage collector is enabled or disabled, it can be called manually. Simply calling (gc) triggers the garbage collector:

(let total (gc))
(print "Number of gargabe collected: " total "\n")


In the example below, a list of 1,2,...,1000000 created and referenced by a variable 'a'. Then a is set to NULL and generated garbage is collected manually.

(gc off)
(let limit 1000000)
(print "Creating array of 0..." limit "\n")
(let a (: 0 limit))
(print "Array created with length " (length a) "\n")
(dump)
(let a NULL)
(print "Gargabe Collecting manually:\n")
(gc)
(dump)


The output is

Creating array of 0...1000000
Array created with length 1000001
Environment Deep: 0
# Sub Environments: 0
# Tokens 1000054
Gargabe Collecting manually:
Environment Deep: 0
# Sub Environments: 0
# Tokens 67


 In the example above, there are 1000054 garbage objects before manual garbage collection. After garbage collecting, there are 67 objects which includes the source code itself. It was a nice experiment to implement a garbage collector in Fuzuli. Hope you have fun with it!

Sunday, July 29, 2012

How to Change Main/Start Screen on Android?

Hello! As you known, there is nothing to say about developing  and progressive Android all around the world. In this situation, we can find any idea to make application. Actually, this is not so easy :) Namely what I am trying to say is that Android provides us to create applications and be a developer in mobile world.

I am sure that most of us have played a game via our mobile phones. Things I said above were for any application actually. Because, games, software about educations, politics, health education, voluntariness, etc applications  allow Android skills.

If you want to create an application to be used by everyone (this is very assertive :) ), you have to be different than other apps. This difference should be about design, software or your idea. 

In this article, I'll show you some codes about tiny difference, "starting page" on your Android application.

I want to tell you about my mind. Users download your app and install it on the phone. The next step will be starting your application. This step is so important. Because, people who use the app decide how good the app is. For that reason, how to start the app should be very important for us. In this context, I can start to code about this.

I've got two classes: Main.java and startingPage.java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startingPagexml);
        
        Thread MyScreen = new Thread() {
         public void run() {
          try {
           sleep(5000);
     
           startActivity(new Intent(getApplicationContext(),startingPage.class));
          } catch (InterruptedException e) {
           e.printStackTrace();
          }
          finally {
           finish();
          }
         }
        };
        MyScreen.start();
    }


The code given above is the first page will be opened on the app. Thread "MyScreen" helps to go another page. When the app opened, wait 5 second and go to the startingPage.class. startingPage.class includes startingPagexml.xml Android Xml file. This file calls the page after 5 seconds.


public class acilis2 extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  Toast.makeText(this, "Welcome to My Application", Toast.LENGTH_LONG).show();
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }


Toast.makeText(this, "Welcome to My Application", Toast.LENGTH_LONG).show();


This code introduces how to give user message after 5 second. If you want to see and learn more information about Toast notification, you can visit android developer official guide in here.


See you next article!

Thursday, July 19, 2012

Multithreading in Fuzuli Programming Language

Since our last commit, Fuzuli supports multithreading. This is the latest milestone that Fuzuli reached in revision tree of 0.1.x.

Fuzuli's multithreading capability stands on the well known boost threading library which will be a built-in package in next C++ standard.

Creating and running a thread in Fuzuli is easy. Define a function and create a thread for this function then call the thread_join method. The join method will wait until the function finishes its job. More than one threads can run at the same time in a time-sharing manner. Multithreading functions are stored thread.nfl, which is now a standard in Fuzuli API.

Lets give an example:


(require "/usr/lib/fuzuli/nfl/thread.nfl")

(function f (params)
 (block
  (print "F started\n")
  (foreach i in (: 1 10)
   (print "f\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function g (params)
 (block
  (print "G started\n")
  (foreach i in (: 1 10)
   (print "g\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function h (params)
 (block
  (print "H started\n")
  (foreach i in (: 1 10)
   (print "h\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(let t0 (thread "f"))
(let t1 (thread "h"))
(let t2 (thread "g"))

(thread_join t0)


In the example given above, we have three function f(), g() and h(). Those functions print messages "F started", "G started" and "H started" at the top of their bodies. A foreach loop then count from 1 to 10 and wait 100 milliseconds after each step. The output is shown below:

F started
H started
G started
g
h
f
g
h
f
g
h
f
g
h
f
g
h
f
f
g
h
f
h
g
g
f
h
g
h
f
f
g
h

Functions in this example run simultaneously. thread_join was called on t0, so whenever the function f() finishes its job, program ends. thread_yield is called for giving a chance to run to another threads. thread_sleep waits for given milliseconds if needed.That's all.