(1) Fill the "9". Code:

* *
|/////|
|/////| * *
|/////| | |
|/////| | |
* * |//9//| | 0 |
| | |/////| | |
| 0 | |/////| | |
*-----* *-----* *-----*
4 9 U

(2) Pour "9" into "4". Code:

* *
| |
| | * *
| | | |
|/////| | |
* * |/////| | 0 |
|/////| |//5//| | |
|//4//| |/////| | |
*-----* *-----* *-----*
4 9 U

(3) Empty "4", Code:

* *
| |
| | * *
| | | |
|/////| | |
* * |/////| | 0 |
| | |//5//| | |
| 0 | |/////| | |
*-----* *-----* *-----*
4 9 U

(4) Pour "9" into "4". Code:

* *
| |
| | * *
| | | |
| | | |
* * | | | 0 |
|/////| | | | |
|//4//| |//1//| | |
* - - * * - - * * - - *
4 9 U

(5) Pour "9" into "U". Code:

* *
| |
| | * *
| | | |
| | | |
* * | | | |
|/////| | 0 | | |
|//4//| | | |//1//|
*-----* *-----* * - - *
4 9 U

(6) Empty "4". Code:

* *
| |
| | * *
| | | |
| | | |
* * | | | |
| | | 0 | | |
| 0 | | | |//1//|
*-----* *-----* *-----*
4 9 U

(7) Fill "9". Code:

* *
|/////|
|/////| * *
|/////| | |
|/////| | |
* * |//9//| | |
| | |/////| | |
| 0 | |/////| |//1//|
*-----* *-----* *-----*
4 9 U

(8) Pour "9" into "4". Code:

* *
| |
| | * *
| | | |
|/////| | |
* * |/////| | |
|/////| |//5//| | |
|//4//| |/////| |//1//|
*-----* *-----* *-----*
4 9 U

(9) Pour "9" into "U". Code:

* *
| |
| | * *
| | | |
| | |/////|
* * | | |/////|
|/////| | 0 | |//6//|
|//4//| | | |/////|
*-----* *-----* *-----*
4 9 U

. . ta-*DAA!*

I used 18 gallons from the water supply

. . and discarded 8 gallons.

But I don't know if that's the optimal solution.