Marine Corps Commandant P.X. Kelley looked out upon hundreds of Marines, soldiers, sailors, airmen and Coast Guardsmen formed before him yesterday on a steamy Fort Myer parade ground. At least five had passed out from the heat. "I had a long speech," he began, "but the troops have been out in the sun for an hour."

His written speech, which he did not deliver, was a scorcher. It blasted lawmakers who believe "you can buy defense on the cheap" and those in the news media with "a lynch-mob mentality."

Kelley was the closing speaker at a ceremony marking his retirement and that of Gen. John A. Wickham Jr., Army chief of staff. Both retire at the end of this month, each with more than 37 years of service.

In his prepared remarks released before the ceremony, Kelley said the first of his "haunting concerns" was "a growing attitude in the Congress which places more credence in the view of staff members on matters dealing with national security than in the views of the services' chiefs. This attitude is driving a wedge between the members of Congress and the nation's principal military advisers. We must reverse this trend."

Although Kelley did not mention it, the usually friendly House Armed Services Committee became so furious at him after the 1983 bombing of a Beirut barracks, in which 241 Marines died, that it drafted a resolution calling for his resignation. Several committee members said Kelley had misled them. In the end, the committee shelved the resolution.

Kelley said his second worry "is with the feeling among some members of our Congress that you can buy the defense of our country on the cheap."

"My final concern," he said, "is with the unbalanced treatment of our armed forces by many, not all, but many members of our 'Fourth Estate.' For one who has spent the past 37 years in building an institution, it is particularly difficult for me to understand a lynch-mob mentality, one which appears devoted to destroying the public image of our most precious institutions.

"One can only ask the question," Kelley said, " 'Is this what our forefathers envisioned by the First Amendment to our Constitution?' I think not. I ask responsible members of the media to strive for balance and objectivity as they discharge their obligations to the American people. Irresponsible sensationalism has no place among men of honor and integrity."

The commandant did not cite examples, but friends has said he has been distressed by many of news accounts of the alleged sex-for-spying activities of Marines guarding the U.S. Embassy in Moscow.

Wickham, in his farewell remarks, said the military services have gone a long way over the past four years to widen cooperative arrangements. He said it was a "myth" that the Joint Chiefs of Staff, consisting of the chiefs of all four services plus a chairman and vice chairman, have a "you scratch my back and I'll scratch yours" approach to their responsibilities.

"If we went to war tomorrow, we would go as joint and coalition forces," Wickham said. "The services must strengthen jointness."