Skip to content

Instantly share code, notes, and snippets.

@rdpoor
Last active August 2, 2017 05:35
Show Gist options
  • Save rdpoor/42829a3d224a151172e1b9668468bfbb to your computer and use it in GitHub Desktop.
Save rdpoor/42829a3d224a151172e1b9668468bfbb to your computer and use it in GitHub Desktop.
Minimal, Complete (within reason) and Verifiable example for https://stackoverflow.com/q/45450583/558639

Demonstration of "p5.serialserver leaking memory"

This gist contains the files to demonstrate the problem described in p5.serialserver leaking memory.

Description

Run the Arduino code

To run this, you'll need an "Arduino-like device" with a USB port capable of generating serial data continually. The provided sketch (sketch.ino) shows one way do to that. Connect your Arduino-like device to your host computer (e.g laptop or desktop), compile and flash sketch.ino into your Arduino-like device, and start it running.

Run the Server code

Next, you'll need to fire up the p5.serial server. Here are instructions on how to do that. In this example, we'll assume that you've chosen the command line version. It will probably look something like this:

$ sudo npm install -g p5.serialserver
$ p5erial

Run the client code

Copy sketch.js (from this gist) and index.html (from this gist) into the directory of your choice. Download p5.js and p5.serialport into that directory. Launch the web browser of your choice (I've been using Chrome) and open index.html. Also open the Developer Tools or whatever your browser provides for viewing the Javascript Console.

What to observe

Open the process viewer of your choice. (On Mac OSX it's called Activity Monitor.) Find the server process (in my case, it was just called 'node').

On the Javascript console, you should see a string of text with an ever-increasing prefix. You should observe that the numbers update quickly at first, but then become more and more sluggish as the server's memory size grows and grows.

<html>
<head>
<meta charset="UTF-8">
<script language="javascript" type="text/javascript" src="p5.js"></script>
<script language="javascript" type="text/javascript" src="p5.serialport.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<style> body {padding: 0; margin: 0;} </style>
</head>
<body></body>
</html>
/**
* sketch.ino is a minimal sketch to generate lots of serial output from Arduino(like)
* device to a host via USB.
*/
int count;
// setup() is called once at initialization
void setup() {
Serial.begin(9600); // baud rate is actually ignored for USB
count = 0;
}
// loop() is called repeatedly
void loop() {
// the content of string isn't important (this is what my app was printing)
Serial.print(count++);
Serial.print(", ");
Serial.println("97.51, 97.53, 89.65, 89.70, 49.73, 49.54, 9.11, 9.06");
delay(2); // minimal delay
}
// node.js client code to run on host computer.
var serial;
var portName = "/dev/cu.usbmodem1411";
var options = {
baudrate: 115200
};
function setup() {
serial = new p5.SerialPort();
serial.open(portName, options);
serial.on('data', gotData);
}
// Called when there is data available from the serial port
function gotData() {
console.log(serial.readLine());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment