Had this chat with my orthopedic surgeon a few years ago- after my fifth knee surgery. Sounds like I have the little Rice Krispie dudes living in my knees. Couple of reasons he pointed out. Synovial fluid—basically the fluid that lubricates our joints… It holds gases, and when knees (or other joints) have rested and then are get used again, those gases are released and “popping” is the common noises you’d hear. (If you go to a chiropractor, that noises that your spine/neck makes when being adjusted is the release of gases too.)
Another reason is the rubbing of ligaments against bones and articular cartilage. If ligaments aren’t stretched properly or enough, that rubbing creates noises.
Wearing down of cartilage is another common reason, but you’d likely experience some pain if that were the case, so it’s probably the release of gas or the ligament rubbing.