Articles on: Troubleshooting

Why is my CPU at 100%? Optimization tips

Why is my CPU at 100%? Optimization tips


Watching the CPU indicator hit 100% on the Square Cloud dashboard can be a sign that your application is running at its limit or that there is a critical inefficiency in your code. When an application consumes all available processing power, it can experience slowdowns, freezes, or automatic restarts by the platform.



1. What does "100% CPU" mean?


Unlike RAM, which functions as a "storage space", the CPU is the execution engine. If it is at 100%, it means the processor is dedicating every single available cycle to process the instructions your application is sending, leaving no time to "rest".



2. Common Causes of High Consumption


A. Infinite Loops or "Busy Waiting"

This is the most common mistake, especially in bots. It happens when you create a loop that runs as fast as possible without a pause.

  • Error: while True: pass (Python) or while(true) {} (JS).
  • Solution: Always add a small delay to free up the processor.
    • Python: await asyncio.sleep(1)
    • Node.js: Use setInterval or pauses with Promises.


B. Synchronous Code in Heavy Operations

Frameworks like Node.js are built on a single thread. If you execute a highly intensive function (such as cryptography or image processing) synchronously, you block the event loop, causing the CPU to spike.

  • Solution: Use asynchronous functions (async/await) and, for heavy mathematical tasks, consider using Worker Threads.


C. Processing Libraries

Some audio processing libraries (for music bots) or image manipulation tools (Canvas/Sharp) are naturally demanding. If your plan has low CPU resources, these tasks will quickly hit the limit.



3. Optimization Strategies


For JavaScript Developers (Node.js)

  • Avoid fs.readFileSync: Prefer the asynchronous version so you don't freeze execution while the disk is being read.
  • Monitor Event Listeners: Too many pending event listeners can generate unnecessary processing.
  • Efficient Middleware: In Express/Fastify, avoid complex middlewares that run on every route unnecessarily.


For Python Developers (Flask/FastAPI/Discord.py)

  • Leverage asyncio: If you are using discord.py or Nextcord, avoid using the requests library (synchronous). Prefer aiohttp.
  • Optimize List Comprehensions: In data processing, use generators and efficient structures to avoid redundant loops.



4. How to Identify the Problem


  1. Logs Tab: Check if errors are being triggered repeatedly. Often, an application trapped in an error loop consumes a lot of CPU trying to restart failed processes.
  2. Metrics Graphs: Observe whether the CPU spike coincides with the exact moment the bot joins a voice channel or when the webserver receives a specific request.



5. When Code is Not the Problem


If you have already optimized your code, removed unnecessary loops, and the CPU remains at 100%, your application may have simply outgrown its current resources.


  • Scalability: If your bot is in thousands of servers or your API receives hundreds of simultaneous requests, the required processing power naturally increases.
  • Plan Upgrade: Consider upgrading to a Standard or Pro plan, which offer more vCPUs.

Updated on: 05/22/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!