服务器开发优化

① tomcat优化(1GB内存的机子)
https://www.cnblogs.com/centos2017/p/9956432.html
https://cloud.tencent.com/developer/article/1463754
在catalina.sh文件中,找到cygwin=false,在这一行的前面加入具体如下并保存退出:

JAVA_OPTS="-server -Xms256m -Xmx768m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=256m"

② 图片等资源文件不要放在tomcat的webapps目录下,放在专门存放资源文件的云存储,然后只要记录路径于数据库即可,或者放在webapps目录之外。
https://ask.csdn.net/questions/507863

③ 发现堆内存走势如上图,正常情况下堆内存走势应该是折线,而该图表示,直到堆内存满了之后,系统执行了full gc 堆内存使用率才下降,即发生了内存泄漏,这个问题令我百思不得其解,然后我利用mat排查原因,发现是框架问题,猜测spring在默认配置下,不会释放对象,但确确实实影响了我并发速度,查阅资料发现在主监听器千增加

IntrospectorCleanupListener

监听器可解决该问题 于是在web.xml中添加:
【亲测仅增加IntrospectorCleanupListener可以正常访问】

<!-- 防止内存泄露 -->
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

再增加下面的就不行了,所以注释掉

<!--<listener>
    <description>Spring 核心监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>-->

https://blog.csdn.net/qq_20948277/article/details/79158602

④ tomcat开机自启动:【亲测有效】
https://blog.csdn.net/hwijew/article/details/83586921
这个脚本是使用者自定的开机启动程序,可以在里面添加想在系统启动之后执行的脚本或者脚本执行命令
修改脚本文件rc.local:vim /etc/rc.d/rc.local
在最后一行增加(换成你实际tomcat的路径):
/home/tomcat包名/bin/startup.sh start
esc 退出编辑,:wq 保存修改

⑤ tomcat在生产环境多次热部署APP导致内存不断被消耗最终溢出的问题:
方案1:一个项目使用一个tomcat,部署后重启该tomcat,不会对其他项目产生影响
方案2:使用tomcat-docker
https://stackoverflow.com/questions/42265522/memory-leak-issue-in-deploying-multiple-tomcat-application-in-cpanel-server

Are you in production environment?
Exception appear exactly at deployment stage or at the first usage of your app?
Your apps are ligth (microservices) or are monolithics apps ? Xms and -Xmx arguments depend of that.
When you deploy a war file in tomcat, some section of tomcat java memory are used and is not well-managed by tomcat. A proof of that is if you deploy several times the same app without restart, an outofmemory appear in tomcat log.

If you are in production environment:

Use one tomcat by app. So you can deploy, stop, start, restart, etc this app without risk of impact other apps. If you restart tomcat after one deploy, memory errors at deployment stage go away.

Use tomcat-docker. With docker your could kill the entire tomcat ad start a new instance in each deploy. So your tomcat would be clean.

https://www.cnblogs.com/areyouready/p/8949991.html

Guess you like

Origin blog.csdn.net/ithouse/article/details/102513355