How To Solve The Error
First of all, make sure you've tried lowering your in-game video graphics. High video graphics will consume more RAM which is what we are trying to avoid. This did improve the performance of the game in my case as it was loading into servers faster.
So there are multiple things you could try doing and here's everything i've tried:
Step 1
Open your library in steam>Right click on the game from the library>Properties>Set Launch Options. Now there are multiple commands you would want to try for this one. Here's a list of the commands i tried.
-dxlevel 80
-dxlevel 90
-dxlevel 95
The following commands are used to downgrade your DirectX used for the game to DirectX 8 or DirectX 9 (Mine was DirectX 12)
FOR GARRY'S MOD ONLY
+gmod_mcore_test 1 -gl
This other command however, makes the game run on OpenGL instead of DirectX, suggested to me by a fellow user who also said "Opengl is known to give FPS boosts to Garry's Mod and that other launch parameter I added turns your Garry's Mod multithreaded so it helps your FPS more"
Now try launching the game again. Still doesn't work? Well here's something else you could try.
Step 2
IF YOU DON'T HAVE AN NVIDIA GRAPHICS CARD IGNORE THIS STEP
First of all, make sure your Graphics card has the latest update. This can by opening GeForce Experience, if you don't have it here's a download link from geforce's official website
https://www.geforce.com/geforce-experience/download
Now once you've got it installed and all, open GeForce Experience (preferred to run as administrator) > Then Click on DRIVERS in the top left corner. Click on "Check for Updates" in the top right, it will automaticall download if there's an update but it won't install automaticall, you have to click Install on that window.
The next step won't harm but i do it just in case. GeForce Experience usually detects the games you have on your Computer
There's an option where you can choose to optimize the game so in GeForce Experience:
Click on HOME > Go ahead to the game with the problem (Garry's Mod in my case). You'll find a green button that has OPTIMIZE written on it. Click on that and lets move on to the next step
Go to NVIDIA Control Panel (if you can't find where that is, go to your computer's Control Panel then search for NVIDIA Control Panel)
Click on Manage 3D Settings>Program Settings>Add (then add the game with the problem, if it's Garry's Mod then you'll find it named hl2.exe)
Now once you've added the game, you'll notice there's "2. Select the preferred graphics processor for this program:"
Choose Integrated Graphics since it also reduces the consumption of RAM for the game. Unlike the High-performance NVIDIA processor option.
Click on Apply.
Now after doing all this try launching the game and see if its working.