I have a spring boot application and its memory us...
# getting-started
d
I have a spring boot application and its memory usage is constantly growing. How to start looking what might be the cause of this?
From ~440 MB to ~460 in 5 hours
s
Profiler, JVM metrics, etc
j
That looks quite flat to me, except for a spike followed by a GC that makes the memory go down to lower than what it was at the beginning of the chart. I don’t think there is any reason to freak out. Consuming memory is normal, and the GC doesn’t necessarily reclaim it if there is no pressure.
c
What are you actually graphing here? I can’t see the y axis so don’t know for certain what metric you’re showing or what the scale is.
In general, I’d recommend starting by attaching visualvm to the Java process (you’ll need to enable JMX in your app and you’ll need to be able to connect to the box that’s running your app from the box that you’re running visualvm on) and looking at garbage collection and where your memory is being used. None of this is Kotlin specific, though, so you may not be asking about this in the right place.
d
I let it run for a while and situation got worse 😐
At the times where memory usages increases I'm generating sitemap of my website and uploading it to S3 bucket, like this:
Copy code
val tags = mutableSetOf<String>()

        users.forEach { user: User ->
            user.tags.forEach { tag: String ->
                tags.add(tag)
                tags.add(tag.substring(tag.lastIndexOf("/") + 1))
            }
        }

        val sitemapMain: ByteArray = createUrlSet {
            addUrl(websiteName!!)
            tags.sortedBy { it }.forEach { tag: String ->
                addUrl("${websiteName}/${tag}", LocalDate.now())
            }
        }.toString().toByteArray()

        applicationEventPublisher.publishEvent(
            UploadSitemapEvent(
                this,
                sitemapMain,
                "sitemap.xml"
            )
        )
Event:
Copy code
override fun onApplicationEvent(event: UploadSitemapEvent) {
        val logger = KotlinLogging.logger {}

        <http://logger.info|logger.info> { "Started uploading ${event.sitemapName} to bucket: $bucket" }

        val s3Client = AmazonS3ClientBuilder.standard()
            .withRegion(Regions.EU_CENTRAL_1)
            .withCredentials(
                AWSStaticCredentialsProvider(
                    BasicAWSCredentials(
                        access,
                        secret
                    )
                )
            )
            .build()

        val targetStream: InputStream = ByteArrayInputStream(event.sitemap)

        val metadata = ObjectMetadata()
        metadata.contentType = "application/xml"

        val request: PutObjectRequest = PutObjectRequest(
            bucket,
            event.sitemapName,
            targetStream,
            metadata
        ).withCannedAcl(CannedAccessControlList.PublicRead)

        s3Client.putObject(request)

        <http://logger.info|logger.info> { "Finished ${event.sitemapName} uploaded to bucket: $bucket" }

    }
Although it didn't increase in usage at 12:04 where another sitemap generation event happend I'm confused
Since I was told that JVM will free memory when needed, I guess I'll try putting my trust into it for now
s
My observations: 1. Creating a new s3 client on each event. 2. Seems like you are forgetting to close
targetStream: InputStream
? I can be wrong, but think you need to close it.
👍 2
d
Thanks! I'll look into these things later today.
c
It still isn’t completely clear what metric the graph is showing. Is this memory usage in the JVM heap? If so, I’d expect the line to go up until a certain size is reach, then for the garbage collector to run and for the number to come back down again and to start slowly rising until the garbage collector runs again. Seeing the memory usage in the heap go up between GCs isn’t bad in of itself. What is bad is seeing the size after each GC be a little higher each time. That’s what tells you that you’re leaking memory. If that’s happening then a free tool like visualvm can be useful in showing you where your memory (and CPU and other things) is going. (Apologies if I’m telling you stuff that you already know here.)
1