String memory usage - bit of a newbie question

In my sketch I have a Serial.println("a 50 byte string") - it's actually a script and parameters to be run by a remote system.

According to the "stats" printed at the end of the compile this is taking up 62 bytes of ROM and 50 bytes of RAM (determined by commenting out the line)

Increasing the string to 60 bytes increases the ROM usage by 72 and the RAM by 60 bytes.

Does this mean that any strings I have in my program take the same amount of space in RAM and ROM? If so it seems rather inefficient use of resources.

I've had a look at the PROGMEM directive, but all the examples I can find copy the string to RAM first before printing.

Are there any alternative solutions to this?

TIA

Does this mean that any strings I have in my program take the same amount of space in RAM and ROM?

Yes.

If so it seems rather inefficient use of resources.

How so? The strings have to be stored in ROM (Flash memory). After all, that is the only place that initialization data can be stored. The strings are, when the Arduino boots up, copied to SRAM, for ease of access and so you can overwrite parts of them, as needed.

You can prevent the copy to SRAM by wrapping the string in the F() macro.

Hmmm ... yes the F() macro seems to do exactly what I want :slight_smile:

However, I'm a bit surprised that "const" strings and string literals are copied -- yes, I'm aware that ROM access is likely to be slower, but in my case I dont really care.

I'm aware that ROM access is likely to be slower, but in my case I dont really care.

How is the compiler to know that?