Sunday, February 6, 2011

How to avoid your desktop being idle, while you are not at the seat


As the title implies the aim of this project was to create a small app to make the desktop mouse move continuously for a specified time while you are away from the PC.

How to use the app

Below figure 1.1 shows a snap shot of the app. As you can see you can specify time duration in minutes (integer value) in the empty text field and press start to make your mouse move !!! …  You can only type digits on that text filed and if you try to add any characters in it, it will discard the characters instantly as you press the character key. You are not permitted to start the app while the text field is empty. If so the app will pop out a warning massage (figure 1.2) and will wait till you specify time duration in minutes.The progress bar will show how much time left for the app to continue. After the specified time is over the app will destroy itself giving the user full freedom to use its mouse again.


Figure 1.1 : Robo_Mouse App

Figure 1.2 : Warning massage 

It should be noted that after you start the app it takes the control of the mouse over from the user, and if you want to stop it, you can’t use the mouse now as it’s currently handled by the Robot Mouse app.

How to stop the running app

If you want to exit from the app before the app exits by itself simply press Alt and f4 keys together (Alt + f4).  If that doesn’t work for some reason go to task manager and kill the app. If something extremely goes wrong  as with most other apps restarting your machine will end up the controversy. (As this is just a simple program that has been written for my own interest I won’t take responsibility for any issues arise regarding any damage done to any PC or any electronic device).



Lessons I learnt from this…

In order to display time in every second three main distinct methods of coding been experimented.  Priority was given to the code that can use less objects and consume lesser memory space. Because its been assumed that this app would be used while there are important apps running on the machine.

As the application should run for a user defined time interval , the app should be using the memory  cleverly, otherwise creating many date objects for getting the current time would end up with lots of garbage in the heap. In order to monitor the memory consumption for the selected three alternative methods for updating time, I ran them separately in a while loop for 100000 times.

code snippets for three methods I experimented with are as follows..

        // method 1 ...

        int i = 0;
        Date d = null;
        Calendar c = Calendar.getInstance();

        while (i < 100000) {

            d = new Date();
            c.setTime(d);
            c.getTime();
            i++;

        }

-----------------------------------------------------------------------------------------------------

       // method 2 ...

       int i = 0;
       Calendar c = Calendar.getInstance();

        while (i < 100000) {

            c.setTimeInMillis(System.currentTimeMillis());
            c.getTime();
            i++;

        }

-----------------------------------------------------------------------------------------------------        
        
        // method 3 ...

        int i = 0;
        Calendar c = Calendar.getInstance();
        SimpleDateFormat formatter;
        java.util.Date utilDate = null;

        while (i < 100000) {

            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DATE);
            int hour = c.get(Calendar.HOUR);
            int min = c.get(Calendar.MINUTE);
            int sec = c.get(Calendar.SECOND);


            String date = year + "/" + month + "/" + day + "  " + hour + ":" + min + ":" + sec;


            try {
                formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
                utilDate = formatter.parse(date);

            } catch (ParseException e) {
                System.out.println(e.toString());

            }

            i++;

        }
    }


-----------------------------------------------------------------------------------------------------


The results were significant…

Method for updating time
Used Heap  (Bytes)
Method 1
4,155,904 (depicted in figure 2.1)
Method 2
4,222,864 (depicted in figure 2.2)
Method 3
4,107,760 (depicted in figure 2.3)


Figure 2.3
Figure 2.1
Figure 2.2

It’s been evident & confirmed that creating Date objects in every loop seems costly in the methods. In method 1 creating Date objects was straight forward. In method 2, Date objects been created implicitly by the c.getTime(). I would rather so thank full to anyone who can give a far more better way of dealing this scenario avoiding creating Date objects. As you can see in the code snippets all those anonymous Date objects will be floating in the heap till GC (garbage Collector) collects them.

Resources :
http://www.odi.ch/prog/design/newbies.php
http://netbeans.org/download/magazine/01/nb01_profiler.pdf


you can download the source codes or the app from here ...
If you need to only run the app just download the below .jar file and double click it to execute...

Source codes : Source Codes of Robot Mouse
Robo_Mouse App (Robot_Mouse.jar) : Download RobotMouse App

No comments:

Post a Comment