Linux executable global and other memory - how to reduce global memory space options

I am trying to reduce the global variables mainly two fold:

  1. To decrease the time to load in memory and increase program start execution time
  2. Optimize memory for global variables

I am not sure how to approach to resolve the same. Couple of questions is :

a) How do I know how much memory global variables in a executable is consuming?

b) How to separate global variable and load it efficiently in memory - like is it feasible to make use of shared libs / class variables etc?

What options I should use to see global variables using nm utility - anything else that can aid my query

Please share your viewpoint on same.

Hi.

You are talking about a Linux application you are developing written in C++?

Is that correct?

Yes, the application is written using C++ 14

Hey @satyaprakashprasad

I'm not an expert C++ developer so sorry to sound a bit "out of touch".

My thought is "why are you using global variables" in the first place?

I thought using global variables in a C++ application is considered "poor programming".

From my side, I am currently developing a fairly complex Ruby on Rails application and I have only one global variable, which I plan to "get rid off" after I complete the app.

Its a existing application that I am trying to analyze and make changes. First of all I need to know what is the memory global variables is taking - how much time it takes to load in memory . I understand that nm utility can provide the same but I am not sure

Then what we we can reduce its memory usage and replace global variables efficiently?

Hi @satyaprakashprasad

Which complier are you using to compile your application?

Seems to me you the first step to analyze and debug this is using the complier.

What is the exact complier you are using @satyaprakashprasad?

C++ compiler version used is MinGW C++ 8.1 / GCC-8.3.1.

Please let me know steps like -

How much space is global memory occupying or comes to in term of size
Approach to replace global variables
How much time does it takes to load these global variables in memory before the application starts executing