Guard your memory with Valgrind

Valgrind is an apt-and-play (or rpm-and-play) tool for finding memory leaks in your sw.

I recommend using it for code-reviewing and before deploying!

Just run it agains # ls -d /tmp

# valgrind  --leak-check=full --track-origins=yes ls -d /tmp

The output is [extract]

==27666== Memcheck, a memory error detector
==27666== Command: ls -d /tmp
/tmp
==27666== HEAP SUMMARY:
==27666==     in use at exit: 12,653 bytes in 8 blocks
==27666==   total heap usage: 1,396 allocs, 1,388 frees, 77,395 bytes allocated
==27666==
==27666== 120 bytes in 1 blocks are definitely lost in loss record 7 of 8
==27666==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==27666==    by 0x40CFACD: getdelim (in /lib/tls/i686/cmov/libc-2.10.1.so)
==27666==    by 0x405B73A: ??? (in /lib/libselinux.so.1)
==27666==    by 0x4064BFC: ??? (in /lib/libselinux.so.1)
==27666==    by 0x40530C7: ??? (in /lib/libselinux.so.1)
==27666==    by 0x400D8BB: ??? (in /lib/ld-2.10.1.so)
==27666==    by 0x400DA20: ??? (in /lib/ld-2.10.1.so)
==27666==    by 0x400088E: ??? (in /lib/ld-2.10.1.so)
==27666==
==27666== LEAK SUMMARY:
==27666==    definitely lost: 120 bytes in 1 blocks
==27666==    indirectly lost: 0 bytes in 0 blocks
==27666==      possibly lost: 0 bytes in 0 blocks
==27666==    still reachable: 12,533 bytes in 7 blocks
==27666==         suppressed: 0 bytes in 0 blocks
==27666== Reachable blocks (those to which a pointer was found) are not shown.
==27666== To see them, rerun with: --leak-check=full --show-reachable=yes
==27666==
==27666== For counts of detected and suppressed errors, rerun with: -v
==27666== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 25 from 8)


Funnily we found ONE leak ;)

==27666== LEAK SUMMARY:
==27666==    definitely lost: 120 bytes in 1 blocks
==27666==    indirectly lost: 0 bytes in 0 blocks

ad it’s in

==27666== 120 bytes in 1 blocks are definitely lost in loss record 7 of 8
==27666==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==27666==    by 0x40CFACD: getdelim (in /lib/tls/i686/cmov/libc-2.10.1.so)
==27666==    by 0x405B73A: ??? (in /lib/libselinux.so.1)
...
==27666==    by 0x400088E: ??? (in /lib/ld-2.10.1.so)

but this time we shouldn’t care too much: ls is a one-shot command!

Comments welcome!

Managing dependencies with maven

Maven is a nice build tool helper for java projects. A cool feature is the way to specify if some dependencies are

  1. necessaries at runtime
  2. provided by the application server
  3. necessaries while doing tests but not at deploy time

That’s an example in my pom.xml:

<!– I need to embed javamail in my package –>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>

<!– while Log4j is provided by the application server –>

<dependency>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
<!– and  Hypersonic Database is used for testing–>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.1</version>
<scope>test</scope>
</dependency>

Cure insanity with Selenium!

“insanity is to keep repeating the same thing and expecting different results”

This Einstein quote comes to my mind each time I’ve to face functional testing (aka repetitive clicking on each webapp link). SeleniumHQ is a framework that automatize this job. It’s based on 2 components:

  1. Selenium IDE: a firefox plugin that “record” the test to be done, and save it as a java/python/perl program;
  2. Selenium RC: a java (no tomcat) server which helps to execute the script.

Example: let’s create a test to verify our login application

  1. download/unzip the framework in you PC and run the server
  2. run firefox selenium plugin
  3. click on the “record” red button (similar to the MSOffice one for recording macros)
  4. go to our webapp url, and submit login form, then click on “check mail”
  5. click on “save” button, choosing your file format (eg. python)
  6. save the file in the server folder as testWebmailLogin.py
  7. edit testWebmailLogin.py and change the basic url (eg. webmail.example.com)
  8. run testWebmailLogin.py
  9. check output

Selenium will run a firefox instance for you, and you’ll see the browser repeating the action you recorded!

Obviously you can use selenium to stress your webapp too!!! A quickstart is here..