Unable to use all ram available?

timl12332
Posts: 27
Joined: Wed May 10, 2017 3:05 pm

Unable to use all ram available?

Postby timl12332 » Thu Aug 09, 2018 3:01 pm

Hi! 291

I have a ILI9341 display I am drawing a GUI too, which works ok. But I can see the display refresh(it redrawing the graphs and such).
So I thought of creating a buffer inside the ESP32 to draw too, so I can then directly push all the pixels to the display when the buffer is filled.
This is where the problem is, with the function:

Code: Select all

Serial.print((ESP.getFreeHeap()/1000));
I can print the available heap mem in KB, this returns 291KB.
So I did some simple math to calculate how much memory I need, I need one byte per color, so:
320*240*3 should do the job.
Meaning a display of 320*240 with 1 byte per pixel.
This results in 230.4KB of memory needed. Should fit I guess since 291KB is available...
So I try this:

Code: Select all

    if (malloc(320*240*3) == 0)
    {
      Serial.println("FAILED TO ALLOCATE MEM");
      while(1){}
    }
And was surprised to see it return "FAILED TO ALLOCATE MEM" and it having not allocated anything.

So my question is, what's up with above? Why doesn't it fit?
Is there a limit on how much memory one can use per task? Is the ESP.getFreeHeap function bugged?

Thanks!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Unable to use all ram available?

Postby kolban » Thu Aug 09, 2018 3:08 pm

Howdy,
The problem isn't that there isn't enough memory available ... the problem is that there isn't enough contiguous free memory available to satisfy your request as a single contiguous block. When you call malloc(), if it returns successfully, you are assured that the data starts at the address returned for the length you requested.

Now imagine that you have 291KB available ... but it exists as three chunks of 100KB, 100KB and 291KB. If you asked for a chunk that is 230KB in size, it will fail as you don't have that. What you might be able to get away with is allocating more but smaller chunks.

For example, if you have 240 rows of 320 columns x 3 bytes then you could allocate 240 chunks of 320 x 3 and save the pointers to these chunks in a 240 element array. However, this all assumes that your logic doesn't need the memory as a contiguous chunk.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

timl12332
Posts: 27
Joined: Wed May 10, 2017 3:05 pm

Re: Unable to use all ram available?

Postby timl12332 » Sat Aug 11, 2018 10:55 am

kolban wrote:Howdy,
The problem isn't that there isn't enough memory available ... the problem is that there isn't enough contiguous free memory available to satisfy your request as a single contiguous block. When you call malloc(), if it returns successfully, you are assured that the data starts at the address returned for the length you requested.

Now imagine that you have 291KB available ... but it exists as three chunks of 100KB, 100KB and 291KB. If you asked for a chunk that is 230KB in size, it will fail as you don't have that. What you might be able to get away with is allocating more but smaller chunks.

For example, if you have 240 rows of 320 columns x 3 bytes then you could allocate 240 chunks of 320 x 3 and save the pointers to these chunks in a 240 element array. However, this all assumes that your logic doesn't need the memory as a contiguous chunk.
Thanks! Sorry for the late reply.
But then how about I want a array of size 240*320*3 of char's? Then I get a compiler error that it can't fit.
Is that the same problem?

The thing is that I am used to be able to reserve large chucks of ram as I can on the PC.(not that large in this case)

Would an array of arrays work?(2d array?)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Unable to use all ram available?

Postby kolban » Sat Aug 11, 2018 3:00 pm

When you attempt to allocate an array of bytes ...

static char myData[240*320*3]

You are asking for a contiguous chunk of storage. Whether that storage is allocated from the stack, from .data, form .bss or from the heap ... the request is still contiguous storage. While the ESP32 has 512K of storage, some of that is reserved for ESP32 operation (eg. cache from flash). However, the architecture of the ESP32 breaks the RAM up into distinct chunks. See the following:

1.3.2 - Embedded memory

ESP32 Technical Reference

https://www.espressif.com/sites/default ... ual_en.pdf

If I am reading it correctly, the theoretically largest chunk of RAM that you can allocate as a unit is 200KBytes. However, in practice, it will be significantly less than that due to prior reservations from ESP-IDF before your own app gets control.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Unable to use all ram available?

Postby Deouss » Sat Aug 11, 2018 5:22 pm

Maybe use WROVER Esp with 4mb psram. I was able to allocate full chunk of 4MB with PSRAM flag for heap malloc.
That memory was used for same ILI9341 display but 16bit colors.

timl12332
Posts: 27
Joined: Wed May 10, 2017 3:05 pm

Re: Unable to use all ram available?

Postby timl12332 » Sat Aug 11, 2018 5:28 pm

Deouss wrote:Maybe use WROVER Esp with 4mb psram. I was able to allocate full chunk of 4MB with PSRAM flag for heap malloc.
That memory was used for same ILI9341 display but 16bit colors.
Interesting, only is 4mb not too much for 16bit?

My 4mb module is on the way.

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Unable to use all ram available?

Postby Deouss » Sat Aug 11, 2018 5:47 pm

Yes - I was just testing memory allocation. 16bit is 320*240*2=153.6kb. I was using buffer for extra images
By the way I am interested in jpeg decompressor that can unpack image into one single ram chunk.
There is idf example but the function returns an array of chunks.

timl12332
Posts: 27
Joined: Wed May 10, 2017 3:05 pm

Re: Unable to use all ram available?

Postby timl12332 » Sat Aug 11, 2018 5:50 pm

Deouss wrote:Yes - I was just testing memory allocation. 16bit is 320*240*2=153.6kb. I was using buffer for extra images
By the way I am interested in jpeg decompressor that can unpack image into one single ram chunk.
There is idf example but the function returns an array of chunks.
Well, you can modify it to output one chunk. But I think I'm just gonna keep it in multiple chunks.

Who is online

Users browsing this forum: No registered users and 47 guests