The number of memory devices in the results of sudo dmidecode --type 17 is equal to the number of memory slots, so the command to print the number of RAM slots is:
sudo dmidecode --type 17 | grep 'Memory Device' --count
The results of this command will be one integer number equal to the number of RAM slots.
The command to show the size of each of the installed RAM sticks is:
sudo dmidecode --type 17 | grep -i size
This is a very informative command because it shows the number of empty RAM slots, the number of RAM slots that have RAM sticks installed and the size of each installed RAM stick.
The command to show the maximum RAM capacity (the maximum size that you can increase the RAM to) is:
sudo inxi -m | grep capacity
This example output shows that the computer has 4 RAM slots (2 empty slots & 2 full slots), and two 4GB RAM sticks (8GB RAM).
$ sudo dmidecode --type 17 | grep 'Memory Device' --count
4
$ sudo dmidecode --type 17 | grep -i size
Size: No Module Installed
Size: No Module Installed
Size: 4096 MB
Size: 4096 MB
$ sudo inxi -m | grep capacity
Array-1 capacity: 32 GB devices: 4 EC: None
dmidecode shows the number of slots available for the controller. There are up to 4 DMI types, 2 can be memory arrays, and 2 can be specific slots. 2 are less common. The information in dmidecode/inxi is extremely unreliable when it comes to the array data, but the actual slot data is generally very reliable. If you are looking into buying or upgrading RAM, you should always check the product specifications to make sure that what inxi/dmidecode reports is actually correct. This goes in particular for maximum RAM size and array capacities.*