Building folly on low-memory arm64 systems suffers high memory pressure due to -g in default Release CXX flags
Problem
Folly's default CMAKE_CXX_FLAGS_RELEASE includes -g debug symbols.
This greatly increases object file size and memory consumption during compilation.
On low-memory arm64 environments such as:
- Termux running on mobile ARM64 phones
- FreeBSD arm64 package build servers
The huge memory footprint brings a high risk of OOM killer terminating compiler jobs in the middle of building. This failure case has already been observed on FreeBSD's official package builder for folly.
Reproduction
Build folly on low RAM arm64, using default CMAKE_BUILD_TYPE=Release.
The generated compile command contains -O3 -DNDEBUG -g.
During compilation, the peak memory usage rises sharply, and the build process gets killed by OOM once exceeding available RAM.
Suggestion
Add an optional CMake toggle (e.g. FOLLY_RELEASE_WITH_DEBUG_SYMBOLS) to control whether -g is added in Release builds.
Meta's server use case can keep -g enabled by default, while embedded / low-memory users can turn it off.
Current Workaround
Override CMAKE_CXX_FLAGS_RELEASE manually when configuring:
-DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG"
This removes -g, shrinks object file size and lowers peak memory pressure to avoid OOM.
Build log snippet
When compiling with default Release flags, each compilation command contains -g:
[146/689] /data/data/com.termux/files/usr/bin/c++ -DFMT_SHARED -DFOLLY_XLOG_STRIP_PREFIXES="/data/data/com.termux/files/home/folly:/data/data/com.termux/files/home/folly/builda" -DGFLAGS_IS_A_DLL=0 -DGLOG_USE_GLOG_EXPORT -D_GNU_SOURCE -D_REENTRANT -I/data/data/com.termux/files/home/folly -I/data/data/com.termux/files/home/folly/builda -I/data/data/com.termux/files/home/folly/builda/_deps/fastfloat-src/include -O3 -DNDEBUG -g -Wall -Wextra -O3 -std=gnu++20 -g -finput-charset=UTF-8 -fsigned-char -Wall -Wno-deprecated -Wno-deprecated-declarations -Wno-sign-compare -Wno-unused -Wuninitialized -Wunused-label -Wunused-result -DGLOG_USE_GLOG_EXPORT -Wno-nullability-completeness -Wno-inconsistent-missing-override -fopenmp -pthread -MD -MT folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufIovecBuilder.cpp.o -MF folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufIovecBuilder.cpp.o.d -o folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufIovecBuilder.cpp.o -c /data/data/com.termux/files/home/folly/folly/io/IOBufIovecBuilder.cpp [147/689] /data/data/com.termux/files/usr/bin/c++ -DFMT_SHARED -DFOLLY_XLOG_STRIP_PREFIXES="/data/data/com.termux/files/home/folly:/data/data/com.termux/files/home/folly/builda" -DGFLAGS_IS_A_DLL=0 -DGLOG_USE_GLOG_EXPORT -D_GNU_SOURCE -D_REENTRANT -I/data/data/com.termux/files/home/folly -I/data/data/com.termux/files/home/folly/builda -I/data/data/com.termux/files/home/folly/builda/_deps/fastfloat-src/include -O3 -DNDEBUG -g -Wall -Wextra -O3 -std=gnu++20 -g -finput-charset=UTF-8 -fsigned-char -Wall -Wno-deprecated -Wno-deprecated-declarations -Wno-sign-compare -Wno-unused -Wuninitialized -Wunused-label -Wunused-result -DGLOG_USE_GLOG_EXPORT -Wno-nullability-completeness -Wno-inconsistent-missing-override -fopenmp -pthread -MD -MT folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufQueue.cpp.o -MF folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufQueue.cpp.o.d -o folly/io/CMakeFiles/folly_io_iobuf_obj.dir/IOBufQueue.cpp.o -c /data/data/com.termux/files/home/folly/folly/io/IOBufQueue.cpp
Steps to reproduce
- Install Termux on Android ARM64
- Clone folly source code
- Use cmake to configure build: cmake -DCMAKE_BUILD_TYPE=Release ..
- Start compile with ninja, during compilation.
Expected behavior
Release build should NOT add -g debug symbol flag by default.
Only optimization -O3 -DNDEBUG should be present.
Actual behavior
The compiler command line contains -g flag.
Release binary carries DWARF debug symbols. It increases object file size and consumes huge memory during linking, easily trigger OOM on Termux ARM64.
Environment
- Platform: Android ARM64, Termux
- clang version 21.1.8
- Folly source: latest main branch
I've traced where the -g flag comes from in the current main branch.
There are two places injecting -g:
- In
CMake/FollyCompilerUnix.cmake:
set(CMAKE_CXX_FLAGS_COMMON "-g -Wall -Wextra")
This common flag applies to ALL build types, then gets appended into CMAKE_CXX_FLAGS_RELEASE :
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_COMMON} -O3")
2. The function apply_folly_compile_options_to_target adds another -g via target_compile_options:
target_compile_options(
${THETARGET}
PRIVATE -g
...
)
The existing workaround -DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG" only overrides the CMAKE_CXX_FLAGS_RELEASE variable.
It cannot remove the -g added by target_compile_options for individual targets.
So there is still a leftover -g even after applying that workaround on current main branch.
To fully eliminate -g and reduce compile-time memory pressure:
1. Remove -g from CMAKE_CXX_FLAGS_COMMON
2. Delete the PRIVATE -g line inside apply_folly_compile_options_to_target
Otherwise the target-level -g remains and still consumes extra memory during compilation on low-memory arm64 systems like Termux.
Original Document Text
~/folly $ cat CMake/FollyCompilerUnix.cmake
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(CMAKE_CXX_FLAGS_COMMON "-g -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_COMMON}")
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_COMMON} -O3")
list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
function (apply_folly_compile_options_to_target THETARGET)
target_compile_definitions(${THETARGET} PRIVATE _REENTRANT _GNU_SOURCE)
target_compile_options(
${THETARGET}
PRIVATE -g
-finput-charset=UTF-8
-fsigned-char
-Wall
-Wno-deprecated
-Wno-deprecated-declarations
-Wno-sign-compare
-Wno-unused
-Wuninitialized
-Wunused-label
-Wunused-result
${FOLLY_CXX_FLAGS})
endfunction ()
~/folly $
Hi @1234562026, I’d like to work on this issue.
I’ll investigate the -g flags added through both CMAKE_CXX_FLAGS_COMMON and apply_folly_compile_options_to_target, verify the impact across supported build configurations, and implement a clean CMake fix with appropriate build/test validation.
Hello. is there any progress on this? There doesnt seem to be any linked PR in recent weeks.
@1234562026 I'd like to pick this up. I have been able to replicate the issue locally. Will provide an MR shortly.