Mister Q
Posts: 5
Joined: Wed Mar 02, 2016 11:56 am

Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 12:26 pm

Respected colleagues,

I read some voltage levels with an A/D converter continuously with Raspberry Pi 2 trough the SPI, store measurement data to a RAM array and, at the end of measurement, log results on the external hard drive. Everything work flawlessly if the amount of data is under 8 MB. If an array become bigger then 8 MB, I got "Segmentation Fault" error.

At the same time, Raspberry Pi 2 has about 950 MB of free RAM ("Free Memory Size: 970895360 Bytes").

Can somebody tell me how to use all available RAM memory of the Raspberry Pi 2 for mentioned purpose, or point me to a some code example ?

User avatar
RaTTuS
Posts: 10549
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 12:40 pm

post your code
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Mister Q
Posts: 5
Joined: Wed Mar 02, 2016 11:56 am

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 1:39 pm

RaTTuS wrote:post your code
Here is the code. It doesn't matter do I split an array in two or more smaller arrays. When the cumulative maximum size is reached, an error pop-up when I try to build and deploy the application to a Raspberry Pi 2.

Code: Select all

// Work OK
#define ADC_CHANNELS	8		// Number of ADC channels
#define RECORD_LENGTH	524000	// 524000 samples (times 8 channels) is the array maximum size
signed short ADC_Measurements[RECORD_LENGTH][ADC_CHANNELS];
memset(ADC_Measurements, 0, sizeof(ADC_Measurements));

// Error "Segmentation Fault"
#define ADC_CHANNELS	8		// Number of ADC channels
#define RECORD_LENGTH	530000	// Number above the maximal size of an array
signed short ADC_Measurements[RECORD_LENGTH][ADC_CHANNELS];
memset(ADC_Measurements, 0, sizeof(ADC_Measurements));

// Error "Segmentation Fault"
#define ADC_CHANNELS	8		// Number of ADC channels
#define RECORD_LENGTH	265000	// Number above the half of the maximal size of an array
signed short ADC_Measurements_1[RECORD_LENGTH][ADC_CHANNELS];
signed short ADC_Measurements_2[RECORD_LENGTH][ADC_CHANNELS];
memset(ADC_Measurements_1, 0, sizeof(ADC_Measurements_1));
memset(ADC_Measurements_2, 0, sizeof(ADC_Measurements_2));

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25977
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 1:43 pm

What happens if you manually calculate thesarray sizes.

ie

Code: Select all

#define ADC_CHANNELS   8      // Number of ADC channels
#define RECORD_LENGTH   265000   // Number above the half of the maximal size of an array
signed short ADC_Measurements_1[RECORD_LENGTH][ADC_CHANNELS];
signed short ADC_Measurements_2[RECORD_LENGTH][ADC_CHANNELS];

size_t s = ADC_CHANNELS * RECORD_LENGTH * sizeof(signed short);

memset(ADC_Measurements_1, 0, s);
memset(ADC_Measurements_2, 0, s);

Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25977
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 1:54 pm

Also, with large arrays like that, it would be worth allocating them from the heap, you might be exceeding stack sizes if they are being allocated on it. if they are static probably not a problem, but still, I would recommend allocation.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

LeoWhite
Posts: 131
Joined: Mon Apr 08, 2013 9:20 am
Contact: Website

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 1:56 pm

Mister Q wrote:

Code: Select all

// Work OK
#define ADC_CHANNELS	8		// Number of ADC channels
#define RECORD_LENGTH	524000	// 524000 samples (times 8 channels) is the array maximum size
signed short ADC_Measurements[RECORD_LENGTH][ADC_CHANNELS];
memset(ADC_Measurements, 0, sizeof(ADC_Measurements));
It looks like you are declaring these variables on the stack, which has a default size of 8MB

Code: Select all

[email protected] ~ $ ulimit -s
8192
You could validate this by increasing the stack size to 16MB and seeing if that makes the Seg fault happen later.

Code: Select all

[email protected] ~ $ ulimit -s 16384
But long term you're probably better off switching over to using malloc and free to dynamically allocate large chunks of memory, or perhaps reading the results in blocks and writing the 'completed' blocks out to disk whilst the next block is filling up.

Leo

Mister Q
Posts: 5
Joined: Wed Mar 02, 2016 11:56 am

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 3:40 pm

jamesh wrote:What happens if you manually calculate thesarray sizes.

size_t s = ADC_CHANNELS * RECORD_LENGTH * sizeof(signed short);
The same. "Received a SIGSEGV: Segmentation fault"

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25977
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 3:41 pm

Mister Q wrote:
jamesh wrote:What happens if you manually calculate thesarray sizes.

size_t s = ADC_CHANNELS * RECORD_LENGTH * sizeof(signed short);
The same. "Received a SIGSEGV: Segmentation fault"
Stack exceeded.

malloc the data.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

Mister Q
Posts: 5
Joined: Wed Mar 02, 2016 11:56 am

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 3:52 pm

LeoWhite wrote: It looks like you are declaring these variables on the stack, which has a default size of 8MB

You could validate this by increasing the stack size to 16MB and seeing if that makes the Seg fault happen later.

Code: Select all

[email protected] ~ $ ulimit -s 16384
I have tried that, but result is the same: "Received a SIGSEGV: Segmentation fault"

[email protected] ~ $ ulimit
unlimited
[email protected] ~ $ ulimit -s 16384

Seems that size is unlimited.
But long term you're probably better off switching over to using malloc and free to dynamically allocate large chunks of memory, or perhaps reading the results in blocks and writing the 'completed' blocks out to disk whilst the next block is filling up.
Can you please post a snippet of code how to do this? Where and how to malloc and where and when to free?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 25977
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 3:56 pm

Not tried...

Code: Select all

#define ADC_CHANNELS   8      // Number of ADC channels
#define RECORD_LENGTH   265000   // Number above the half of the maximal size of an array
signed short *ADC_Measurements_1;
signed short *ADC_Measurements_2;

size_t s = ADC_CHANNELS * RECORD_LENGTH * sizeof(signed short);

ADC_Measurements_1 = (signed short*) malloc(s);
ADC_Measurements_2 = (signed short*) malloc(s);

memset(ADC_Measurements_1, 0, s);
memset(ADC_Measurements_2, 0, s);

// Use the data, and when finished...

free(ADC_Measurements_1);
free(ADC_Measurements_2);
Or use calloc to avoid doing the memset

http://linux.die.net/man/3/calloc
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“My wife said to me `...you’re not even listening`.
I thought, that’s an odd way to start a conversation.."

Mister Q
Posts: 5
Joined: Wed Mar 02, 2016 11:56 am

Re: Raspberry Pi 2 RAM usage

Wed Mar 02, 2016 4:59 pm

jamesh wrote:Not tried...

Code: Select all

#define ADC_CHANNELS   8      // Number of ADC channels
#define RECORD_LENGTH  59375000    // Can be a 950 MB (59,375,000 * 8)
signed short *ADC_Measurements;

size_t s = ADC_CHANNELS * RECORD_LENGTH * sizeof(signed short);

ADC_Measurements = (signed short*) malloc(s);

memset(ADC_Measurements, 0, s);

// Use the data, and when finished...

free(ADC_Measurements);
YES, that is the right solution!

Now, I can use all the RAM on the Raspberry Pi 2:
#define RECORD_LENGTH 59375000 // Can be a 950 MB (59,375,000 * 8)

THANKS A LOT !

Return to “C/C++”