为了调查内存泄漏,我设置了一条在global.gc()
POST /gc
的路由
app.post('/gc', function(req, res){
global.gc();
});
但是,我注意到如果我发送此请求,它每次都会越来越多地减少内存使用量。我们不应该调用global.gc()
一次就足以将内存减少到最低?
如果是这样,为什么连续多次呼叫会在每次通话时减少内存?
(我使用Node.js v0.12)
答案 0 :(得分:0)
在非常高的水平上,V8将GC分为两部分。寻找要收集的垃圾,然后实际收集垃圾。调用import React, { useState } from 'react'
import styled from 'styled-components'
import GooglePlacesAutocomplete from 'react-google-places-autocomplete'
const AutoComplete = props => {
const StyledDiv = styled.div`
width: 300px;
`
const [address, setAddress] = useState('')
const autocompletionRequest = {
componentRestrictions: {
country: 'US',
},
types: ['(regions)'],
}
const onSelect = ({ description }) => {
setAddress(
description
.split(', ')
.slice(0, -1)
.join(', ')
)
}
return (
<StyledDiv>
<GooglePlacesAutocomplete
placeholder="Search City"
initialValue={address}
onSelect={onSelect}
autocompletionRequest={autocompletionRequest}
suggestionsStyles={{
container: {
color: 'red',
},
suggestion: {
background: 'black',
},
}}
renderSuggestions={(active, suggestions, onSelectSuggestion) => (
<div className="suggestions-container">
{suggestions.map(suggestion => (
<div
className="suggestion"
key={suggestion.id}
onClick={event => onSelectSuggestion(suggestion, event)}
>
{suggestion.description
.split(', ')
.slice(0, -1)
.join(', ')}
</div>
))}
</div>
)}
/>
</StyledDiv>
)
}
export default AutoComplete
仅做第二部分,收集已经发现的垃圾。