Many calls to the garbage callector when copying values from an array to another with type casting

Adrien COURNAND 141 Reputation points
2021-03-05T09:57:37.66+00:00

I am creating an application that needs to make fft on audio data. For the process part, the audio data need to be cast to double. So I wrote this function:

public void processRecordedData<PlatformRecordFormat> (PlatformRecordFormat[] platformRecordedData)
{
     Array.Copy(platformRecordedData, recordedData, MICROPHONE_SETUP.SAMPLE_COUNT);

    //Processing audio
}

And this function is called from Xamarin.Android with a short[] as input:

private void processAudioData()
{
    do
    {
        ProcessAudioDataEvent.WaitOne();
        pianoTrainingPageInstance.processRecordedData(audioBuffers[processIndex]);
        processIndex = (processIndex + 1) % AUDIO_BUFFER_COUNT;
    } while (audioRecord != null);
 }

recordedData and audioBuffers[processIndex] are both created and with the same size.

The problem is that I have a lot of messages from the garbage collector that never stop popping on the application output windows:

[Mono] GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 7 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.04ms tarjan 0.01ms scc-setup 0.04ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
[Mono] GC_BRIDGE: Complete, was running for 0.07ms
[Mono] GC_MINOR: (Nursery full) time 1.17ms, stw 1.92ms promoted 0K major size: 1408K in use: 665K los size: 1024K in use: 654K

It's always those 3 lines. I tried to switch my function from template parameter to short[], I tried copying with a for loop, but there are always those 3 messages.

From what I understand, copying the short array to the double performs memory allocation and because this function is called often, the application goes soon out of memory and the GC have much memory to free. But I don't understand why? Maybe for each element of the short array, a double is created with new and then returned with the converted value? In this case, is there a way to perform a copy without allocating memory? I am from a c++ background so maybe I am doing something wrong by trying yo mimic what I would do in c++.

When I comment the Array.Copy line, the messages disappear.

The messages appear wether I am on real hardware or in the emulator, but with the emulator, I have this in addition :

[zygote64] Background concurrent copying GC freed 1330(2MB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1496KB/2MB, paused 8.911ms total 32.277ms

(It might be unrelated because I have this kind of message from the emultor whether or not I comment the Array.Copy line)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
{count} vote

Accepted answer
  1. Adrien COURNAND 141 Reputation points
    2021-03-15T08:22:04.883+00:00

    As many of you suggest I wrote a for loop and this time the messages are gone. I change a bit the design of my application, I don't have anymore the templated function. I don't know why in the template version even the for cause GC messages, but now it's done, thank you all

    0 comments No comments

0 additional answers

Sort by: Most helpful