grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 950 forks source link

Grails3.3.9 as JSON variable dont GC #11609

Open zhangjiayu139 opened 4 years ago

zhangjiayu139 commented 4 years ago

Thanks for reporting an issue for Grails framework, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (http://stackoverflow.com/tags/grails) or Slack (http://slack-signup.grails.org). DO NOT use the issue tracker to ask questions.

Defines a utils class, Used to convert domain instances into Map, The following code: public static def domainFormat(domain) { def result = domain as JSON try{ return (Map)JSON.parse(result.toString()) }catch(e){ e.printStackTrace() } } However, in the production environment variables are not recycled(GC),This often results in full memory and FULL CPU, Grab the stack memory of the production environment and analyze all points to this public method image

Task List

Steps to Reproduce

  1. TODO defined utils class:

public static def domainFormat(domain) { def result = domain as JSON try{ return (Map)JSON.parse(result.toString()) }catch(e){ e.printStackTrace() } }

  1. TODO def book = Book.get(1) Map map = domainFormat(book ) map.tttt = "demo" render map as JSON
  2. TODO

Expected Behaviour

Tell us what should happen

Actual Behaviour

Tell us what happens instead

Environment Information

Example Application

davydotcom commented 4 years ago

This is a really odd way to do this. as JSON is actually being deprecated in favor of grails-views. That being said I use as JSON often and have never seen it get stuck in memory like this. It would seem something else is likely holding onto a reference of your object. Or maybe something cyclical exists in your map.

davydotcom commented 4 years ago

The stack implies an instanced closure may be sticking around as well yet your sample code contains no closures

zhangjiayu139 commented 4 years ago

@davydotcom I'm using it in Service, to turn the object into a Map and add other property values,I'm confused, too

puneetbehl commented 3 years ago

@zhangjiayu139 you could use JsonViewTemplateEngine instead of as JSON. See http://views.grails.org/latest/#_the_jsontemplateengine

jeffscottbrown commented 3 years ago

However, in the production environment variables are not recycled(GC)

I don't understand what you mean. Is it the case that every time you do the conversion, a new instance of middol.utils.DomainUtils$_domainListFormat_closure1 is being allocated and never collected? If not, what is it that isn't being garbage collected? Thanks for any feedback!