一件关于Bug的小事
关于内存泄露,我之前一直都没有什么特别的感觉,只是平时看书上说,要关闭文件的句柄,要释放占用的内存资源,于是我就这么做,并没有什么特别的理解,直到这次Java Web的最后项目……
因为这次的项目要做和数据库关联的,其中涉及好几个表,为了代码有一个良好的结构,于是使用了MVC设计模式,对于每一个数据表,都单独抽象出来的一个类,来封装增删查改操作。由于每个表的操作都一样,项目也比较简单,每个类的代码,有很多重复的地方,于是为了偷懒,好多查询的函数,我都没有释放数据库连接的句柄,没想到就是这一偷懒,导致了下面的悲剧。
当时的情况是这样的,整个项目基本已经写完了,开始进行测试,发现网页一开多,项目就崩溃,然后到console上看log,发现由于数据库连接数过多,导致项目崩溃了,这时候,突然意识到,自己在写代码的时候,有好多的连接没关闭,然后,为了修改这个错误,每个函数都看了一遍,然后把该释放的资源都释放掉,再次运行,多开网页,没有问题,全部pass。
本想偷个懒,没想到最后还是要把之前遗漏的补上,这种返工式的修改,反而比直接一步完成,更加费事费力。以后还是少偷懒,多干活……
Little thing about the bug
About the memory leak, I didn't have any special feeling before, except the books i read usually always say you should close the file handlers to release the memory resources, so I do as what the book
say , and havn't had a special understanding until this final Java Web project ......
Because this project should use database, which involves several tables. To make a good code structure, I used the MVC design pattern. For each data table, it is abstracted to a separate class to encapsulate deletions ,selections, updations. Since the operations of each table are the same, the project is relatively simple, each class code has a lot of repeat places, so in order to be easy, I have not released the database connection handlers in many query functions.Havn't realized this idleness led to the following tragedy.
The situation is such that the whole project has basically been finished, begin testing and found if i open more webpages, the project collapsed, and then I watched to console log, found that due to the excessive number of database connections, leading to the collapse of the project, this time and suddenly realized that he was writing code, there are a lot did not close the connection, and then, in order to amend this error, each function read it again, and then release the resources that are freed to run again, to open the page , there is no problem, all passed.
The steal a lazy, did not expect to put on before finally missing complement this type of rework amend, but one step than direct, more protracted and painstaking. After or less lazy, more work ......