Skip to content

Memory Settings

Optimize BrowserStack Code Quality memory allocation for efficient analysis based on codebase size.

Memory Configuration Requirements

For repositories with 2+ million lines of code, proper memory configuration is essential for successful analysis.

Key Memory Settings:

  • Container Memory (-m): Total memory allocated to Docker container
  • Analyzer Heap (ANALYSER_XMX): Memory specifically for analysis engine
  • Risk Engine (RISK_XMX): Memory for risk calculation operations

Memory Recommendations by Codebase Size

Lines of CodeANALYSER_XMXContainer Memory (-m)Recommended Host RAM
Up to 1 Million-Xmx6g12GB16 GB
2-10 Million-Xmx15g30GB32 GB
10+ Million-Xmx30g60GB64 GB
20+ Million-Xmx45g90GB128 GB

Memory Allocation Rules

  1. Keep ANALYSER_XMX ≤ 70% of container memory
  2. ANALYSER_XMX scales proportionally with lines of code (LOC)
  3. ANALYSER_XMX and RISK_XMX are independent, non-overlapping settings
  4. Reserve 20-30% container memory for system operations and UI processes

Memory Percentage Guidelines

Analyzer Memory as Percentage of Total Container Memory:

  • < 1M LOC: 35-40% (allows overhead for UI and system processes)
  • 1-3M LOC: 40-45% (balanced allocation for medium repositories)
  • 3-5M LOC: 50-55% (increased analyzer priority for large codebases)
  • > 5M LOC: 60-70% (maximum allocation for very large repositories)

WARNING

Never exceed 70% allocation for ANALYSER_XMX as this can cause container instability and OOM errors in system processes.

Code Duplication Impact

High Duplication Scenarios:

  • Generated code or auto-generated files in repository
  • Framework dependencies with similar patterns
  • Copy-paste code across multiple modules

Memory Adjustment for Duplication:

bash
# Base recommendation for 1M LOC
ANALYSER_XMX=-Xmx6g

# High duplication adjustment (+20-30%)
ANALYSER_XMX=-Xmx8g

# Repository with significant code duplication (+40-50%)  
ANALYSER_XMX=-Xmx9g

Docker Compose Configuration

yaml
version: '3.8'
services:
  app:
    image: "browserstack/code-quality:1.9.36.0"
    environment:
      - ANALYSER_XMX=-Xmx15g
      - RISK_XMX=-Xmx1024m
    deploy:
      resources:
        limits:
          memory: 30G
        reservations:
          memory: 20G

Docker Run Configuration

bash
docker run -m 30GB -d \
  --name BrowserStackCodeQuality \
  -e ANALYSER_XMX=-Xmx15g \
  -e RISK_XMX=-Xmx1024m \
  -e EMB_ANALYSER_THREADS=8 \
  -p 3000:3000 \
  browserstack/code-quality:1.9.36.0

Memory Distribution Guidelines

Analyzer Memory as Percentage of Container Memory

Repository SizeAnalyzer Memory %System Reserve %
< 1M LOC35-40%25-30%
1-5M LOC40-50%20-25%
5-10M LOC50-60%15-20%
> 10M LOC60-70%10-15%

Memory Monitoring and Optimization

Monitor Container Memory Usage

bash
# Real-time memory monitoring
docker stats BrowserStackCodeQuality

# Memory usage during analysis
docker exec BrowserStackCodeQuality top -p $(pgrep java)

Check Java Heap Usage

bash
# JVM memory information
docker exec BrowserStackCodeQuality jstat -gc $(pgrep java)

# Heap dump for analysis (if needed)
docker exec BrowserStackCodeQuality jmap -dump:format=b,file=/tmp/heapdump.hprof $(pgrep java)

Performance Optimization Strategies

For High Code Duplication

Characteristics:

  • Multiple similar files or libraries
  • Generated code or auto-generated files
  • Large framework dependencies

Adjustments:

  • Increase ANALYSER_XMX by 20-30%
  • Monitor memory usage patterns during analysis
  • Consider excluding auto-generated files from analysis

For Multi-Language Repositories

Memory Impact:

  • Each language parser requires additional memory
  • Complex language interactions increase memory usage
  • Cross-language dependency analysis needs more resources

Recommendations:

  • Add 2-4GB to base ANALYSER_XMX recommendation for multi-language projects
  • Increase EMB_PARSER_THREADS for parallel processing efficiency
  • Monitor per-language analysis performance and adjust accordingly

For C/C++ Projects - Strict Mode Analysis

NOTE

C++ Repositories: For improved analysis accuracy, especially with build integration, consider Remote Scan with Strict Mode. This significantly enhances code quality detection but requires additional memory allocation.

C++ Strict Mode Requirements:

  • Additional Memory: Increase ANALYSER_XMX by 30-50% above base recommendation
  • Build Integration: Requires embold-trace tool for comprehensive analysis
  • Remote Scan Setup: See Integration Guide for detailed configuration

C++ Memory Scaling:

bash
# Standard C++ project (1M LOC)
ANALYSER_XMX=-Xmx6g

# C++ with Strict Mode (1M LOC) 
ANALYSER_XMX=-Xmx8g

# Large C++ project with Strict Mode (5M LOC)
ANALYSER_XMX=-Xmx40g

Troubleshooting Memory Issues

Out of Memory Errors

Symptoms:

OutOfMemoryError: Java heap space
Analysis terminated unexpectedly
Container restart during analysis

Solutions:

  1. Increase ANALYSER_XMX (ensure < 70% of container memory)
  2. Increase container memory limit (-m parameter)
  3. Reduce concurrent analysis threads
  4. Enable garbage collection logging for detailed analysis

Performance Degradation

Indicators:

  • Analysis takes significantly longer than expected
  • High CPU usage with low progress
  • Frequent garbage collection events

Optimizations:

  1. Monitor memory allocation patterns
  2. Adjust thread counts based on available memory
  3. Consider incremental or remote scanning approaches
  4. Use SSD storage to reduce memory pressure from I/O operations

Memory Configuration Testing

bash
# Test memory configuration before major analysis
docker exec BrowserStackCodeQuality java -XX:+PrintFlagsFinal -version | grep HeapSize

# Verify container memory limits
docker inspect BrowserStackCodeQuality | grep -A 3 "Memory"

# Check system memory availability
free -h && echo "Docker Host Memory Available"

# Monitor memory allocation during analysis
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" BrowserStackCodeQuality

# Real-time Java process monitoring
docker exec BrowserStackCodeQuality sh -c 'while true; do ps aux | grep java | grep -v grep; sleep 5; done'

Advanced Memory Tuning

Garbage Collection Optimization:

bash
# Enable GC logging for analysis optimization
docker run -m 30GB -d \
  --name BrowserStackCodeQuality \
  -e ANALYSER_XMX="-Xmx15g -XX:+UseG1GC -XX:+PrintGC -XX:+PrintGCDetails" \
  -e RISK_XMX=-Xmx1024m \
  browserstack/code-quality:1.9.36.0

Heap Dump Collection (for troubleshooting):

bash
# Generate heap dump during OOM issues
docker exec BrowserStackCodeQuality jmap -dump:format=b,file=/opt/gamma_data/logs/heapdump_$(date +%Y%m%d_%H%M).hprof $(pgrep -f "java.*gamma")

# Analyze heap usage patterns
docker exec BrowserStackCodeQuality jstat -gc $(pgrep -f "java.*gamma") 5s

Best Practices

  • Start Conservative: Begin with recommended settings and incrementally adjust based on actual performance data
  • Monitor First Scan: Watch memory usage closely during initial repository analysis to identify bottlenecks
  • Document Optimal Settings: Record successful memory configurations for each major repository or project type
  • Regular Performance Review: Reassess memory needs quarterly as codebases grow or change significantly
  • Test Changes Safely: Validate memory adjustments in non-production environments before applying to production
  • Keep Historical Data: Track memory usage trends over time to predict future scaling needs

Memory Configuration Checklist

Before Analysis:

  • [ ] Verify container memory allocation matches recommendations
  • [ ] Confirm ANALYSER_XMX is ≤ 70% of container memory
  • [ ] Check available host system memory
  • [ ] Review repository characteristics (size, languages, duplication level)

During Analysis:

  • [ ] Monitor container memory usage with docker stats
  • [ ] Watch for OOM errors in application logs
  • [ ] Track analysis performance and completion time
  • [ ] Monitor system resource availability

After Analysis:

  • [ ] Document successful memory settings for this repository type
  • [ ] Record any performance issues or optimization opportunities
  • [ ] Update memory recommendations based on actual usage patterns