Improve this code
<p>Challenge: This has a memory leak due to extend object <code>Receiver r = new DumpReceiver(System.out);</code></p>

<p>Code for DumpReceiver.java
<a href="http://www.jsresources.org/examples/DumpReceiver.java.html" rel="nofollow">http://www.jsresources.org/examples/DumpReceiver.java.html</a></p>

<p>How can I print out raw MIDI code or prevent from creating new object every time a MIDI notes is "Sent", therefore, prevent memory leak?</p>

<pre><code>import java.io.IOException;

import javax.sound.midi.Transmitter;
import javax.sound.midi.Receiver;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;


public class MidiInDump
{
    public static void main(String[] args)
        throws Exception
    {

    String  strDeviceName = "LoopBe Internal MIDI";
    int nDeviceIndex = -1;
    boolean bUseDefaultSynthesizer = false;

    if ((strDeviceName == null) &amp;&amp; (nDeviceIndex &lt; 0))
        {
            out("device name/index not specified!");
        }

    MidiDevice.Info info;
    if (strDeviceName != null) 
        {
            info = MidiCommon.getMidiDeviceInfo(strDeviceName, false);
        }
    else
        {
            info = MidiCommon.getMidiDeviceInfo(nDeviceIndex);
        }
    if (info == null)
        {
            if (strDeviceName != null) 
                {
                    out("no device info found for name " + strDeviceName);
                }
            else
                {
                    out("no device info found for index " + nDeviceIndex);
                }
            System.exit(1);
        }
    MidiDevice  inputDevice = null;
    try
        {
            inputDevice = MidiSystem.getMidiDevice(info);
            inputDevice.open();
        }
    catch (MidiUnavailableException e)
        {
            out(e);
        }
    if (inputDevice == null)
        {
            out("wasn't able to retrieve MidiDevice");
            System.exit(1);
        }
    Receiver r = new DumpReceiver(System.out);
    //Receiver r = MidiSystem.getReceiver();

    //WeakReference&lt;Receiver&gt; wr = new WeakReference&lt;Receiver&gt;(r);

    try
        {
            Transmitter t = inputDevice.getTransmitter();
            t.setReceiver(r);
        }
    catch (MidiUnavailableException e)
        {
            out("wasn't able to connect the device's Transmitter to the Receiver:");
            out(e); 
            inputDevice.close();
            System.exit(1);
        }
    if (bUseDefaultSynthesizer)
        {
            Synthesizer synth = MidiSystem.getSynthesizer();
            synth.open();
            r = synth.getReceiver();
            try
                {
                    Transmitter t = inputDevice.getTransmitter();
                    t.setReceiver(r);
                }
            catch (MidiUnavailableException e)
                {
                    out("wasn't able to connect the device's Transmitter to the default Synthesizer:");
                    out(e); 
                    inputDevice.close();
                    System.exit(1);
                }
        }
    out("now running");

    try
        {
            System.in.read();
        }
    catch (IOException ioe)
        {
        }
    inputDevice.close();
    out("Received "+((DumpReceiver) r).seCount+" sysex messages with a total of "+((DumpReceiver) r).seByteCount+" bytes");
    out("Received "+((DumpReceiver) r).smCount+" short messages with a total of "+((DumpReceiver) r).smByteCount+" bytes");
    out("Received a total of "+(((DumpReceiver) r).smByteCount + ((DumpReceiver) r).seByteCount)+" bytes");
    try
        {
            Thread.sleep(1000);
        }
    catch (InterruptedException e)
        {
        }
}

private static void out(String strMessage)
{
    System.out.println(strMessage);
}



private static void out(Throwable t)
{
        out(t.toString());
}
</code></pre>

<p>}</p>
