Insufficient memory for Java – Sketch on RaspberryPi

Hi there,

I’m running a sketch thats plays random sound every 5 seconds, on a RaspberryPi, using the minim library and the sampler class.
The sketch is running fine but only for 5 minutes, then the sounds starts to deteriorate and finally it crashes and I get this message:
Could not run the sketch

#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 4100096 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /tmp/hs_err_pid1492.log
Java HotSpot(TM) Client VM warning: INFO: os::commit_memory(0x6776f000, 4100096, 0) failed; error='Ne peut allouer de la mémoire' (errno=12)
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help → Troubleshooting.

I understand that the raspberryPi has not enough memory for this sketch to run longer. But I need it to run forever end flawlessly…
There are 99 sounds and their sizes are about 190ko average.

I’m wondering if there’s any king of tricks to empty the memory for java every minutes for example. Or something in the raspberryPi config to change.
I know nothing about those java issues.

Maybe it’s just impossible for a Pi but that would surprise me. Maybe I need to use Python instead.
Hope I might get some help on this.

Here is my code, thanks !

import ddf.minim.*;
import ddf.minim.ugens.*;
import java.util.Date;
AudioPlayer player;
Minim minim;
AudioOutput out;
Sampler     kick;
int rand;
long lastTime = 0;
PFont f;


int max = 99;
IntList songnums = new IntList();
int songidx = 0;
int count,countmax = 99;

void setup() {
  size(200, 200);
  minim = new Minim(this);
   out   = minim.getLineOut();
  for ( int i = 0; i<max; i++) songnums.append(i);
  songnums.shuffle();
  println(songnums);
}

void draw() {
  int time = int(random(5000, 10000));
  if ( millis() - lastTime > time ){
    lastTime = millis();
    rand = songnums.get(songidx);
    songidx++;
    if ( songidx >= max ) songidx = 0;
    String songfile = nf(rand) + ".mp3";
    println("load: "+songfile);
    kick = new Sampler(("./data/" + (nf(rand) + ".mp3")), 4, minim);
    kick.patch(out);
    kick.trigger();
    count++;
  }
  
  if ( count >= countmax ) { 
      count = 0;
      songidx = 0;
      songnums.shuffle(); 
      println(songnums);
    }
}
1 Like

And here is what says the .log file.
(I had to cut the end of it)
Hope it can help.

#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 4100096 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   In 32 bit mode, the process size limit was hit
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Use 64 bit Java on a 64 bit OS
#   Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
#  Out of Memory Error (os_linux.cpp:2749), pid=1492, tid=0x33433470
#
# JRE version: Java(TM) SE Runtime Environment (8.0_202-b08) (build 1.8.0_202-b08)
# Java VM: Java HotSpot(TM) Client VM (25.202-b08 mixed mode linux-arm )
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#

---------------  T H R E A D  ---------------

Current thread (0x76162800):  VMThread [stack: 0x333b4000,0x33434000] [id=1495]

Stack: [0x333b4000,0x33434000]
VM_Operation (0x31eae704): GenCollectForAllocation, mode: safepoint, requested by thread 0x3277b800


---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x31ab8000 JavaThread "Thread-1" [_thread_blocked, id=1522, stack(0x3196d000,0x319bd000)]
  0x31ab3000 JavaThread "Java Sound Event Dispatcher" daemon [_thread_blocked, id=1521, stack(0x31de5000,0x31e35000)]
  0x7610c400 JavaThread "DestroyJavaVM" [_thread_blocked, id=1494, stack(0x762da000,0x7632a000)]
  0x3277b800 JavaThread "Animation Thread" [_thread_blocked, id=1520, stack(0x31e60000,0x31eb0000)]
  0x32779800 JavaThread "AWT-EventQueue-0" [_thread_blocked, id=1519, stack(0x31eb0000,0x31f00000)]
  0x32773800 JavaThread "AWT-Shutdown" [_thread_blocked, id=1518, stack(0x3203f000,0x3208f000)]
  0x32737400 JavaThread "AWT-XAWT" daemon [_thread_blocked, id=1515, stack(0x32348000,0x32398000)]
  0x32722000 JavaThread "Java2D Disposer" daemon [_thread_blocked, id=1514, stack(0x323c6000,0x32416000)]
  0x76194800 JavaThread "Service Thread" daemon [_thread_blocked, id=1512, stack(0x32adf000,0x32b2f000)]
  0x76192000 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=1511, stack(0x32b30000,0x32bb0000)]
  0x32c00800 JavaThread "JDWP Command Reader" daemon [_thread_in_native, id=1505, stack(0x32bb0000,0x32c00000)]
  0x76190800 JavaThread "JDWP Event Helper Thread" daemon [_thread_blocked, id=1502, stack(0x32f10000,0x32f60000)]
  0x7618e000 JavaThread "JDWP Transport Listener: dt_socket" daemon [_thread_blocked, id=1501, stack(0x32f60000,0x32fb0000)]
  0x76184c00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1500, stack(0x32fb0000,0x33000000)]
  0x76169c00 JavaThread "Finalizer" daemon [_thread_blocked, id=1497, stack(0x33313000,0x33363000)]
  0x76168000 JavaThread "Reference Handler" daemon [_thread_blocked, id=1496, stack(0x33363000,0x333b3000)]

Other Threads:
=>0x76162800 VMThread [stack: 0x333b4000,0x33434000] [id=1495]
  0x76196800 WatcherThread [stack: 0x32a5f000,0x32adf000] [id=1513]

VM state:at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread:  ([mutex/lock_event])
[0x761099c0] Threads_lock - owner thread: 0x76162800
[0x76109dd0] Heap_lock - owner thread: 0x3277b800

Heap:
 def new generation   total 219008K, used 205754K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K, 100% used [0x33e00000, 0x3fc20000, 0x3fc20000)
  from space 24320K,  45% used [0x3fc20000, 0x406ee830, 0x413e0000)
  to   space 24320K,  95% used [0x413e0000, 0x42a75488, 0x42ba0000)
 tenured generation   total 495804K, used 495616K [0x49340000, 0x6776f000, 0x73e00000)
   the space 495804K,  99% used [0x49340000, 0x67740088, 0x67740200, 0x6776f000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K

Card table byte_map: [0x33bff000,0x33e00000] byte_map_base: 0x33a60000

Polling page: 0x76fb8000

CodeCache: size=32768Kb used=1730Kb max_used=1730Kb free=31037Kb
 bounds [0x73f00000, 0x740b8000, 0x75f00000]
 total_blobs=904 nmethods=632 adapters=212
 compilation: enabled

Compilation events (10 events):
Event: 325,610 Thread 0x76192000  633             java.nio.Buffer::hasRemaining (17 bytes)
Event: 325,611 Thread 0x76192000 nmethod 633 0x740afcc8 code [0x740afda0, 0x740afe38]
Event: 325,634 Thread 0x76192000  634             java.nio.CharBuffer::hasArray (20 bytes)
Event: 325,635 Thread 0x76192000 nmethod 634 0x740afe88 code [0x740aff60, 0x740b0008]
Event: 351,852 Thread 0x76192000  635             java.util.AbstractCollection::<init> (5 bytes)
Event: 351,853 Thread 0x76192000 nmethod 635 0x740b0048 code [0x740b0120, 0x740b01a8]
Event: 362,563 Thread 0x76192000  639             java.lang.String::indexOf (7 bytes)
Event: 362,564 Thread 0x76192000 nmethod 639 0x740b0808 code [0x740b08f0, 0x740b09d4]
Event: 367,628 Thread 0x76192000  640             java.nio.charset.CharsetDecoder::maxCharsPerByte (5 bytes)
Event: 367,628 Thread 0x76192000 nmethod 640 0x740b0b08 code [0x740b0be0, 0x740b0c68]

GC Heap History (10 events):
Event: 306,035 GC heap after
Heap after GC invocations=61 (full 18):
 def new generation   total 219008K, used 24320K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,   0% used [0x33e00000, 0x33e00000, 0x3fc20000)
  from space 24320K, 100% used [0x3fc20000, 0x413e0000, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 408765K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  84% used [0x49340000, 0x6226f548, 0x6226f600, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
}
Event: 320,433 GC heap before
{Heap before GC invocations=61 (full 18):
 def new generation   total 219008K, used 213623K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,  97% used [0x33e00000, 0x3f6ddd18, 0x3fc20000)
  from space 24320K, 100% used [0x3fc20000, 0x413e0000, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 408765K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  84% used [0x49340000, 0x6226f548, 0x6226f600, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
Event: 320,466 GC heap after
Heap after GC invocations=62 (full 18):
 def new generation   total 219008K, used 17329K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,   0% used [0x33e00000, 0x33e00000, 0x3fc20000)
  from space 24320K,  71% used [0x413e0000, 0x424cc7d8, 0x42ba0000)
  to   space 24320K,   0% used [0x3fc20000, 0x3fc20000, 0x413e0000)
 tenured generation   total 486512K, used 408771K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  84% used [0x49340000, 0x62270ee8, 0x62271000, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
}
Event: 333,531 GC heap before
{Heap before GC invocations=62 (full 18):
 def new generation   total 219008K, used 212017K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K, 100% used [0x33e00000, 0x3fc20000, 0x3fc20000)
  from space 24320K,  71% used [0x413e0000, 0x424cc7d8, 0x42ba0000)
  to   space 24320K,   0% used [0x3fc20000, 0x3fc20000, 0x413e0000)
 tenured generation   total 486512K, used 408771K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  84% used [0x49340000, 0x62270ee8, 0x62271000, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
Event: 333,610 GC heap after
Heap after GC invocations=63 (full 18):
 def new generation   total 219008K, used 21200K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,   0% used [0x33e00000, 0x33e00000, 0x3fc20000)
  from space 24320K,  87% used [0x3fc20000, 0x410d42d0, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 439532K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  90% used [0x49340000, 0x6407b1c8, 0x6407b200, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
}
Event: 343,880 GC heap before
{Heap before GC invocations=63 (full 18):
 def new generation   total 219008K, used 215888K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K, 100% used [0x33e00000, 0x3fc20000, 0x3fc20000)
  from space 24320K,  87% used [0x3fc20000, 0x410d42d0, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 439532K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  90% used [0x49340000, 0x6407b1c8, 0x6407b200, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
Event: 343,976 GC heap after
Heap after GC invocations=64 (full 18):
 def new generation   total 219008K, used 24320K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,   0% used [0x33e00000, 0x33e00000, 0x3fc20000)
  from space 24320K, 100% used [0x413e0000, 0x42ba0000, 0x42ba0000)
  to   space 24320K,   0% used [0x3fc20000, 0x3fc20000, 0x413e0000)
 tenured generation   total 486512K, used 476702K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  97% used [0x49340000, 0x664c78c8, 0x664c7a00, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
}
Event: 357,248 GC heap before
{Heap before GC invocations=64 (full 18):
 def new generation   total 219008K, used 219008K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K, 100% used [0x33e00000, 0x3fc20000, 0x3fc20000)
  from space 24320K, 100% used [0x413e0000, 0x42ba0000, 0x42ba0000)
  to   space 24320K,   0% used [0x3fc20000, 0x3fc20000, 0x413e0000)
 tenured generation   total 486512K, used 476702K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  97% used [0x49340000, 0x664c78c8, 0x664c7a00, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
Event: 357,264 GC heap after
Heap after GC invocations=65 (full 18):
 def new generation   total 219008K, used 11066K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K,   0% used [0x33e00000, 0x33e00000, 0x3fc20000)
  from space 24320K,  45% used [0x3fc20000, 0x406ee830, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 476702K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  97% used [0x49340000, 0x664c7a38, 0x664c7c00, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K
}
Event: 369,959 GC heap before
{Heap before GC invocations=65 (full 18):
 def new generation   total 219008K, used 205754K [0x33e00000, 0x42ba0000, 0x49340000)
  eden space 194688K, 100% used [0x33e00000, 0x3fc20000, 0x3fc20000)
  from space 24320K,  45% used [0x3fc20000, 0x406ee830, 0x413e0000)
  to   space 24320K,   0% used [0x413e0000, 0x413e0000, 0x42ba0000)
 tenured generation   total 486512K, used 476702K [0x49340000, 0x66e5c000, 0x73e00000)
   the space 486512K,  97% used [0x49340000, 0x664c7a38, 0x664c7c00, 0x66e5c000)
 Metaspace       used 7557K, capacity 7758K, committed 7832K, reserved 8496K

Deoptimization events (0 events):
No events

Classes redefined (0 events):
No events

Internal exceptions (10 events):
Event: 0,066 Thread 0x7610c400 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x33e07170) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jni.c
Event: 0,550 Thread 0x7610c400 Exception <a 'java/security/PrivilegedActionException'> (0x340d3398) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jvm.cpp, line 1502]
Event: 0,550 Thread 0x7610c400 Exception <a 'java/security/PrivilegedActionException'> (0x340d3598) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jvm.cpp, line 1502]
Event: 0,551 Thread 0x7610c400 Exception <a 'java/security/PrivilegedActionException'> (0x340d59f0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jvm.cpp, line 1502]
Event: 0,551 Thread 0x7610c400 Exception <a 'java/security/PrivilegedActionException'> (0x340d5bf0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jvm.cpp, line 1502]
Event: 0,727 Thread 0x7610c400 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x341a87a0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/methodHandles.cpp, line 1167]
Event: 0,735 Thread 0x7610c400 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x341b4cd0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/methodHandles.cpp, line 1167]
Event: 0,814 Thread 0x7610c400 Exception <a 'java/io/FileNotFoundException'> (0x3422adc0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jni.cpp, line 710]
Event: 0,815 Thread 0x7610c400 Exception <a 'java/io/FileNotFoundException'> (0x3422b7e8) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jni.cpp, line 710]
Event: 1,958 Thread 0x32779800 Exception <a 'java/lang/UnsupportedOperationException': > (0x33ee6470) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u202/12323/hotspot/src/share/vm/prims/jni.cpp, line 736]

Events (10 events):
Event: 357,268 Executing VM operation: RevokeBias done
Event: 357,268 Executing VM operation: RevokeBias
Event: 357,268 Executing VM operation: RevokeBias done
Event: 362,569 Executing VM operation: BulkRevokeBias
Event: 362,569 Executing VM operation: BulkRevokeBias done
Event: 362,571 Executing VM operation: BulkRevokeBias
Event: 362,571 Executing VM operation: BulkRevokeBias done
Event: 362,577 Executing VM operation: BulkRevokeBias
Event: 362,578 Executing VM operation: BulkRevokeBias done
Event: 369,959 Executing VM operation: GenCollectForAllocation


2 Likes

Hey There !

Did you try increasing memory size in the file > preferences tab?

Yes, I set it to 2048MB. I think it’s already more than the Pi capacity.

1 Like

How about reducing the sounds up to only 50? To see if the issue persists.

Also things such as max and countmax etc… all their values should be given to them in setup. As I believe you can only intalize values outside of methods if they are final or static.

I tried this on PC and visualized memory with VisualVM 1.4.3 and it did keep filling memory every time you loaded a new “kick”.

I tried something different and wrote some quick test code:

import ddf.minim.*;
import ddf.minim.ugens.*;
import java.util.Date;

AudioPlayer player;
Minim minim;
AudioOutput out;
AudioSample kickit;

int count;

void setup() 
  {
  size(200, 200);
  minim = new Minim(this);
  out   = minim.getLineOut();
  }

void draw() 
    {
    String songfile;

    if (frameCount%120 == 0)
      {
      if (kickit != null) kickit.close();
      if (count%2 == 0)
        songfile = "1.mp3";
      else
        songfile = "2.mp3";
    
    println("load: " + songfile);

    kickit = minim.loadSample(songfile, 512);    
    kickit.trigger();
    count++;

    println (count);
//    System.gc();
    }
   }

This is what I see with my code:
image

With system.gc() (it is commented out above) it keeps memory in check:
image

:slight_smile:

2 Likes

This is using the Sampler class.
I did some tinkering and this did not give me any memory issues:
I added a dispose(); to the code.

import ddf.minim.*;
import ddf.minim.ugens.*;
AudioPlayer player;
Minim minim;
AudioOutput out;
Sampler     kick;
int rand;
long lastTime = 0;

int count;

void setup() 
  {
  size(200, 200);
  minim = new Minim(this);  
  }

void draw() 
  {
  int time = 5000;
  if ( millis() - lastTime > time )
    {
//    if (kick != null) kick.stop();
    minim.dispose();
    minim = new Minim(this);
    out   = minim.getLineOut();       
    count++;
    println(count);
    lastTime = millis();
    if (count%2==0)
      kick = new Sampler(("./data/" + ("1" + ".wav")), 4, minim);
    else
      kick = new Sampler(("./data/" + ("2" + ".wav")), 4, minim);
    kick.patch(out);
    kick.trigger();
    System.gc();
    }
  }
1 Like

I looked around and haven’t found a method the set the volume in minim with the sampler method. But I’m quite new to minim
Any suggestions maybe ?

Thanks for your help !

You methods works, but the dispose(); method stop the current file before playing the next one. I forgot to mention that I want the files to play simultaneously. They last approx. 20s average.

I added the System.gc(); line at the end and will try tomorow on my Pi if that changes anything.

Thanks for the help glv !

Hi!

I thought it was a bug that they played simultaneously!

http://code.compartmental.net/minim/javadoc/ddf/minim/ugens/Sampler.html

:slight_smile: