free my peers

Sep 5, 2007 at 7:47am

free my peers

hi list,

trying to hack my way into java (again) i modified the mxj matrixlist example to behave a bit more like the jit.matrixset.

i kind of got it doing what i want (mainly the ability to set a specific matrix that may not have been there before), but i experience the same memory overflow that has been mentioned in earlier threads (that all seem to fade away without conclusion).

one thing i tried is to apply a freePeer(), but am not sure if i chose the correct place to do so (should in be in notifyDeleted, or in the place where i overwrite an existing matrix [the 'set' function that i mainly used]?

the problem only appears with biger matrices, say 4 char 720 576. smaller (or thinner planecount) matrices work fine, even with a bigger amount of matrices.
while the size of matrices remains constant, RAM soon is filled, and matrices are written to the HD, which generally isn’t a good idea (but java seems to disagree on this).

any advice where i could help java with the garbage collection, or any other tip on this dirty hack is greatly welcome!

jan

here the modification of the matrixlist code [originally by bbn]:
—————————————————————–

import com.cycling74.max.*;
import com.cycling74.jitter.*;
import java.util.*;

public class matrixlistMod2 extends MaxObject {

private static class matrixlist_single {
public String name;
public ArrayList matrices;
public int instanceCount;
}
private static ArrayList matrixlists = new ArrayList();

//local references
private ArrayList matrices = null;
private String name = null;

public matrixlistMod2()
{ //no argument- matrixlist will not be public
name = null;
matrices = new ArrayList();
declareReadOnlyAttribute(“size”, “getSize”);
if (matrices.size() == 0)
{
init();
}
}

public matrixlistMod2(String s) { //argument
name = s;
matrices = findName(name);
if (matrices == null)
matrices = addName(name);
else {
int i = findIndex(name);
((matrixlist_single)(matrixlists.get(i))).instanceCount++;
}
declareReadOnlyAttribute(“size”, “getSize”);
if (matrices.size() == 0)
{
init();
}
}

public matrixlistMod2(String s, int listSize) { //argument
name = s;
matrices = findName(name);
if (matrices == null)
matrices = addName(name);
else {
int i = findIndex(name);
((matrixlist_single)(matrixlists.get(i))).instanceCount++;
}
declareReadOnlyAttribute(“size”, “getSize”);
if (matrices.size() < listSize)
{
init(listSize – matrices.size());
}
}

private static int findIndex(String name)
{
int i=-1;
while (++i
if (((matrixlist_single)(matrixlists.get(i))).name.equals(name))
return i;
return -1;
}

public static ArrayList findName(String name)
{
ArrayList al = null;
int i=findIndex(name);
if (i!=-1)
al = ((matrixlist_single)matrixlists.get(i)).matrices;
return al;
}

public static ArrayList addName(String name)
{
matrixlist_single ms = new matrixlist_single();
ms.name = name;
ms.matrices = new ArrayList();
ms.instanceCount = 1;
matrixlists.add(ms);
return ms.matrices;
}

// decrement instance counter
// remove from matrixlists if no instances exist
public static void removeName(String name)
{
int i = findIndex(name);
if (i!=-1)
{
matrixlist_single ms = (matrixlist_single)(matrixlists.get(i));
ms.instanceCount–;
if (ms.instanceCount == 0)
matrixlists.remove(i);
}
}

public void notifyDeleted()
{
removeName(name);
}

public Atom[] getSize() {return new Atom[] {Atom.newAtom(matrices.size())};}

public void add(String s)
{
doInsert(s, matrices.size());
}

public void insert(Atom[] a)
{
if ((a.length>2)||(!a[0].isString())||(!a[1].isInt())) {
error(“matrixlist: bad insert message”);
error(“usage –> insert [matrixname] [index]“);
return;
}
doInsert(a[0].getString(), a[1].getInt());
}

private void doInsert(String matrixname, int index)
{
JitterMatrix j = new JitterMatrix();
j.setinfo(matrixname);
j.frommatrix(matrixname);
matrices.add(j);
}

public void init ()
{
int listSize = 100;
JitterMatrix j = new JitterMatrix();
for(int i = 0; i < listSize;i++)
{
matrices.add(j);
}
}

public void init (int listSize)
{
JitterMatrix j = new JitterMatrix();
for(int i = 0; i < listSize;i++)
{
matrices.add(j);
}
}

public void set(Atom[] a)
{
if ((a.length>2)||(!a[0].isString())||(!a[1].isInt())) {
error(“matrixlist: bad set message”);
error(“usage –> set [matrixname] [index]“);
return;
}
String matrixname = a[0].getString();
JitterMatrix j = new JitterMatrix();
j.setinfo(matrixname);
j.frommatrix(matrixname);
((JitterMatrix)matrices.get(a[1].getInt())).freePeer();
matrices.set(a[1].getInt(), j);
}

public void remove(int index)
{
if ((index > 0)&&(index < matrices.size()))
matrices.remove(index);
((JitterMatrix)matrices.get(index)).freePeer();
/*((JitterMatrix)matrices.get(index)).freePeer(); */

}

public void clear()
{
matrices.clear();
}

public void output(int i) {
if ((i=0)) {
JitterMatrix m = (JitterMatrix)matrices.get(i);
outlet(0, “jit_matrix”, m.getName());
}
}

}

#33538

You must be logged in to reply to this topic.