The degradation, the constant mistakes and mess-ups...It's bad enough to hear see and know how much of a fuck up you are despite how hard you try. It's even worse when your peers and boss validate how much of a fuck up you are and punish you for it in their own degrading, scornful, alienating way. That sort of pain-knowing that everyone hates you and that you are the weak link of the entire group- is enough to drive anyone insane. Especially if you have the emotional fragility of a child.

When you've reached your breaking point, when you can't absorb anything anymore, when you are completely and utterly alone, you go inward into yourself. You stop feeling everything, and your capacity to connect with another person is gone. This state of mind is born out of self defense and preservation- the brains last resort to surviving whatever trauma it has going on against them. You can see it with how Pyle improves as a private after the blanket party, but deteriorates mentally.  

In his case, his new found  numbness resulted in him improving drastically with what the corp wanted of him, but at a huge price: he completely shuts down when it comes to social interaction and norms. He's been stripped to the bare bone to survive. He's been broken. And yes, that's the point of boot camp, but without the support and camaraderie of his peers, he remained broken and became MORE broken. What the drill instructor ended up building back up was a shadow of the man Pyle once was; the picture perfect image of what they were looking for, but underneath dangerously cracked and crumbling. A barely standing condemned structure ready to collapse at any given time.
   
And he did crumble. Not in the bathroom, but shortly beforehand, close enough or during graduation to consciously smuggle in a magazine and plan to sit himself on that toilet to lock and load.

So why did he shoot his DI? Why did he shoot himself? It’s obvious from how Joker found Pyle that Pyle wasn’t in the bathroom for just some alone time with his Charleena. He was arming his weapon to go on an insane bloodbath. Gunnery just happened to be the perfect person he could kill. Think about it.   
 Just imagine what that scene that must have been like to a severely psychotic mind: 

    The root of all of your torment and abuse, continuing to abuse you, except this time you can actually do something about it. You have a weapon. He doesn't. You hold it in your arms, with the knowledge that he's the one who's vulnerable now, and that now you have the upper hand.  You raise your weapon at him, and now he’s shouting at you. You sense his fear, and now you know now for a fact that his threats are empty ones. He holds no power. So you pull the trigger and watch as blood spurts out of him as he goes down.

Seconds pass. The realization that you have finally killed him fuels your insane glee. For a couple of seconds the outside world doesn't exist and you are on cloud nine....until you notice your squadron leader. You start to aim your weapon at him as he slowly puts his hands up in self defense. The glee still runs through you....but it's not the same. He calls you something. A name. The name of someone once upon a time who smiled dimly at the world. A name people called you, not out of contempt or ridicule, but because it was your name. It was your identity. 

You lower your weapon. You think of that name some more with increasing numbness, realizing how that name, that identity isn’t you anymore.  An empty calm chases away any glee left in your system. You feel yourself sit back on the toilet. You completely retreat back into your mind. You see the bathroom, but you’re not SEEING. All you can feel is the emptiness. There’s nothing left to do now. You’ve served your purpose. Charleena has served its purpose. There’s nothing left for you to lose or do. 

So with detached calmness, you put the barrel of your rifle into your mouth and raise it off the ground. One final scream from your squadron leader fills the air as you pull the trigger, mingling with the boom of your rifle.

 

src: https://www.quora.com/Spoiler-Why-did-Private-Pyle-kill-himself-in-Full-Metal-Jacket