Index: trunk/psModules/src/objects/pmSource.c
===================================================================
--- trunk/psModules/src/objects/pmSource.c	(revision 34287)
+++ trunk/psModules/src/objects/pmSource.c	(revision 34315)
@@ -261,21 +261,21 @@
     PS_ASSERT_INT_POSITIVE(Radius, false);
 
-    psRegion srcRegion;
+    psRegion sourceRegion;
 
     // Grab a subimage of the original image of size (2 * outerRadius).
-    srcRegion = psRegionForSquare (x, y, Radius);
-    srcRegion = psRegionForImage (readout->image, srcRegion);
+    sourceRegion = psRegionForSquare (x, y, Radius);
+    sourceRegion = psRegionForImage (readout->image, sourceRegion);
 
     // these images are subset images of the equivalent parents
-    mySource->pixels = psImageSubset(readout->image, srcRegion);
+    mySource->pixels = psImageSubset(readout->image, sourceRegion);
     if (readout->variance) {
-        mySource->variance = psImageSubset(readout->variance, srcRegion);
+        mySource->variance = psImageSubset(readout->variance, sourceRegion);
     }
     if (readout->mask) {
-        mySource->maskView = psImageSubset(readout->mask,  srcRegion);
+        mySource->maskView = psImageSubset(readout->mask,  sourceRegion);
         // the object mask is a copy, and used to define the source pixels
         mySource->maskObj = psImageCopy(NULL, mySource->maskView, PS_TYPE_IMAGE_MASK);
     }
-    mySource->region   = srcRegion;
+    mySource->region   = sourceRegion;
     mySource->windowRadius = Radius;
 
@@ -415,11 +415,11 @@
         int nValid = 0;                 // Number of valid sources
         for (int i = 0; i < sources->n; i++) {
-            pmSource *src = sources->data[i]; // Source of interest
-            if (!src || !src->moments) {
+            pmSource *source = sources->data[i]; // Source of interest
+            if (!source || !source->moments) {
                 continue;
             }
 
 	    if (region) {
-		int x = src->peak->x, y = src->peak->y; // Coordinates of peak
+		int x = source->peak->x, y = source->peak->y; // Coordinates of peak
 		if (x < region->x0 || x > region->x1 || y < region->y0 || y > region->y1) {
 		    continue;
@@ -427,17 +427,17 @@
 	    }
 
-            if (src->mode & PM_SOURCE_MODE_BLEND) {
-                continue;
-            }
-
-            if (!src->moments->nPixels) continue;
-
-            if (src->moments->SN < PSF_SN_LIM) {
+            if (source->mode & PM_SOURCE_MODE_BLEND) {
+                continue;
+            }
+
+            if (!source->moments->nPixels) continue;
+
+            if (source->moments->SN < PSF_SN_LIM) {
                 psTrace("psModules.objects", 10, "Rejecting source from clump because of low S/N (%f)\n",
-                        src->moments->SN);
-                continue;
-            }
-
-            float Mxx = src->moments->Mxx, Myy = src->moments->Myy; // Second moments
+                        source->moments->SN);
+                continue;
+            }
+
+            float Mxx = source->moments->Mxx, Myy = source->moments->Myy; // Second moments
             float ar = Mxx / Myy;       // Radius
 
@@ -1496,2 +1496,51 @@
   return NULL;
 }
+
+// Function to estimate the memory consumed by a source.
+// Not yet complete but it counts the biggest stuff.
+// XXX: handle child images. For big ones the array of data pointers is significant
+#define IMAGE_BYTES(_im, _pix_size) (_im ? (sizeof(psImage) + (_im->numRows * _im->numCols * _pix_size)) : 0)
+#define VECTOR_BYTES(_v, _elem_size) (_v ? (sizeof(psVector) + (_v->n * _elem_size)) : 0)
+
+// estimate the memory consumed by this source
+psU64 pmSourceMemoryUse (pmSource *source) {
+    psU64 bytes = sizeof(pmSource) + sizeof(pmPeak) + sizeof(pmMoments);
+
+    bytes += IMAGE_BYTES(source->modelVar, 4);
+    bytes += IMAGE_BYTES(source->maskObj, 2);
+    bytes += IMAGE_BYTES(source->modelFlux, 4);
+    bytes += IMAGE_BYTES(source->psfImage, 4);
+
+    if (source->modelFits) {
+        for (int i = 0; i < source->modelFits->n; i++) {
+            pmModel *model = source->modelFits->data[i];
+            if (!model) continue;
+            bytes += sizeof(pmModel);
+            bytes += IMAGE_BYTES(model->covar, 4);
+            bytes += VECTOR_BYTES(model->params, 4);
+            bytes += VECTOR_BYTES(model->dparams, 4);
+            if (model->residuals) {
+                bytes += sizeof(pmResiduals);
+                bytes += IMAGE_BYTES(model->residuals->Ro, 4);
+                bytes += IMAGE_BYTES(model->residuals->Rx, 4);
+                bytes += IMAGE_BYTES(model->residuals->Ry, 4);
+                bytes += IMAGE_BYTES(model->residuals->variance, 4);
+                bytes += IMAGE_BYTES(model->residuals->mask, 2);
+            }
+        }
+    }
+    if (source->radialAper) {
+        for (int i = 0; i < source->radialAper->n; i++) {
+            pmSourceRadialApertures *radialAper = source->radialAper->data[i];
+            if (radialAper) {
+                bytes += sizeof(pmSourceRadialApertures);
+                bytes += VECTOR_BYTES(radialAper->flux, 4);
+                bytes += VECTOR_BYTES(radialAper->fluxStdev, 4);
+                bytes += VECTOR_BYTES(radialAper->fluxErr, 4);
+                bytes += VECTOR_BYTES(radialAper->fill, 4);
+            }
+        }
+    }
+
+    return bytes;
+}
Index: trunk/psModules/src/objects/pmSource.h
===================================================================
--- trunk/psModules/src/objects/pmSource.h	(revision 34287)
+++ trunk/psModules/src/objects/pmSource.h	(revision 34315)
@@ -323,4 +323,6 @@
 char *pmSourceModeToString (const pmSourceMode mode);
 
+psU64 pmSourceMemoryUse(pmSource *source);
+
 /// @}
 # endif /* PM_SOURCE_H */
