Tomcat6 on Ubuntu, heap size and perm gen size

I’ve been having some trouble with a server running Tomcat6 on Ubuntu with a Spring/Hibernate set up. It also does some image resizing via Image Magick.

The problems were that the perm gen size for the Hibernate objects was too small and it ended up running out of memory.

No problem, in Ubuntu, the fix was going /etc/default/tomcat6 and editing the JAVA_OPTS variable.

I started off with


That solved the Hibernate problem but I was soon getting out of memory errors for image uploads. I had to add this into the file

JAVA_OPTS="-XX:MaxPermSize=512m -Xms256m -Xmx512m"

Running this gave me some useful feedback about the memory that Java was using.

jmap -heap <pid>

There are some useful resources out there for anyone interested in reading more

PHP upload limits

Every now and then I need to re-set the PHP upload limits in the PHP ini file.

The obvious setting is

upload_max_filesize = 1024M

The less obvious settings are:

file_uploads = On

I’ve found it easy to forget to change post_max_size which need to be the same or greater than the upload_max_filesize.

Zend Framework, styling and decorating a form

Although extending Zend_Form produces a really easy form, the layout that results is less than perfect. This is where subclassing Zend_Form_Decorator_Abstract comes in. Here’s the implementation for a text field.

class Decorators_Inputs extends Zend_Form_Decorator_Abstract
    protected $_format = '
    <div class="element">
        <div class="label_col"
            <label for="%s">%s</label>
         <div class="input_col">
            <input id="%s" name="%s" type="text" value="%s" class="%s" style="%s"/>
         <div class="error_col">
     <br clear="left" />

    public function render($content)
        $element = $this->getElement();
        $name    = htmlentities($element->getFullyQualifiedName());
        $label   = htmlentities($element->getLabel());
        $id      = htmlentities($element->getId());
        $value   = htmlentities($element->getValue());
        $style   = htmlentities($element->getAttrib('style'));

        $class = "";
        $error = "";
        $errors = $element->getErrors();
            $error = 'Please enter a value';
            $class = 'error';

        $markup  = sprintf($this->_format, $name, $label, $id, $name, $value,$class, $style,$error);
        return $markup;

Zend framework, sending an email

Sending an email in the Zend Framework turns out to be reasonably easy. This example uses the contact form for a web site

public function contactAction()
        $request = $this->getRequest();
        $form = new Application_Form_Contact();

       //Check if form has been submitted
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())
              && $this->send($this->getRequest())) {


        $this->view->form = $form;

    private function send(Zend_Controller_Request_Abstract $request)

        $session = new Zend_Session_Namespace();

        if (isset($session->numberOfPageRequests)) {
        } else {
            $session->numberOfPageRequests = 1;

        //pretend mail has been sent for people repeatedly hitting refresh
        if ($session->numberOfPageRequests > 2) {
            return true;

        $email_mapper = new Application_Model_EmailMapper();
        //Get the email text from the database.
        $email = $email_mapper->getEmailByName('ContactEmail', $request);

        $mail = new Zend_Mail();
        $mail->setFrom('', 'Some Sender');
        $mail->addTo('', 'Some Recipient');

        return true;


This is the result of sending the email

I’m using Toolheap’s Test email SMTP server to send test emails during development. It’s proven to be a very useful tool for verifying designs and content of the emails before they are put live.